fstream problem

Write a program that reads in a C++ program from a cpp file, and prints all non-keyword identifiers into a text file. An identifier is a series of characters consisting of letters, digits and underscores (_). Assume that all comments are single-line comments which begin with //. Consider the following program:

// Fig. 6.20: fig06_20.cpp
// Demonstrating C++ Standard Library class template vector.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

void outputVector( const vector< int > & ); // display the vector
void inputVector( vector< int > &, int start ); // input values into the vector

int main()
{
vector< int > integers1( 7 ); // 7-element vector< int >
vector< int > integers2( 10 ); // 10-element vector< int >

// print integers1 size and contents
cout << "Size of vector integers1 is " << integers1.size()
<< "\nvector after initialization:" << endl;
outputVector( integers1 );

// print integers2 size and contents
cout << "\nSize of vector integers2 is " << integers2.size()
<< "\nvector after initialization:" << endl;
outputVector( integers2 );

// input and print integers1 and integers2
cout << "\nEnter 17 integers:" << endl;
inputVector( integers1, 1 );
inputVector( integers2, integers1.size() + 1 );

cout << "\nAfter input, the vectors contain:\n"
<< "integers1:" << endl;
outputVector( integers1 );
cout << "integers2:" << endl;
outputVector( integers2 );

// use inequality (!=) operator with vector objects
cout << "\nEvaluating: integers1 != integers2" << endl;

if ( integers1 != integers2 )
cout << "integers1 and integers2 are not equal" << endl;

// create vector integers3 using integers1 as an
// initializer; print size and contents
vector< int > integers3( integers1 ); // copy constructor

cout << "\nSize of vector integers3 is " << integers3.size()
<< "\nvector after initialization:" << endl;
outputVector( integers3 );

// use assignment (=) operator with vector objects
cout << "\nAssigning integers2 to integers1:" << endl;
integers1 = integers2; // assign integers2 to integers1

cout << "integers1:" << endl;
outputVector( integers1 );
cout << "integers2:" << endl;
outputVector( integers2 );

// use equality (==) operator with vector objects
cout << "\nEvaluating: integers1 == integers2" << endl;

if ( integers1 == integers2 )
cout << "integers1 and integers2 are equal" << endl;

// use square brackets to create rvalue
cout << "\nintegers1[5] is " << integers1[ 5 ];

// use square brackets to create lvalue
cout << "\n\nAssigning 1000 to integers1[5]" << endl;
integers1[ 5 ] = 1000;
cout << "integers1:" << endl;
outputVector( integers1 );

// attempt to use out-of-range subscript
cout << "\nAttempt to assign 1000 to integers1.at( 15 )" << endl;
integers1.at( 15 ) = 1000; // ERROR: out of range
} // end main

// output vector contents
void outputVector( const vector< int > &array )
{
size_t i; // declare control variable

for ( i = 0; i < array.size(); i++ )
{
cout << setw( 12 ) << array[ i ];

if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
cout << endl;
} // end for

if ( i % 4 != 0 )
cout << endl;
} // end function outputVector

// input vector contents
void inputVector( vector< int > &array, int start )
{
for ( size_t i = 0; i < array.size(); i++ )
cin >> array[ i ];
} // end function inputVector

If your program reads in the above program, the contents in the output file should appear as follows:

iostream
iomanip
vector
std
outputVector
inputVector
start
main
integers1
integers2
cout
size
endl
integers3
at
array
size_t
i
setw
cin
---------------------------------------------
bool isKeywords(char string[])
bool legal(char character)
void getIdentifiers(char line[], char identifiers[][32], int &numIdentifiers)
bool isNumber(char string[])
void store(char identifiers[][32], int numIdentifiers)

i dont know how to write. Could someone help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <fstream>
#include <string> 
using namespace::std;
const char keywords[][20] = { "auto", "break", "case", "char", "const", "continue", "default",
"define", "do", "double", "else", "enum", "extern", "float", "for",
"goto", "if", "int", "long", "register", "return", "short",
"signed", "sizeof", "static", "struct", "switch", "typedef",
"union", "unsigned", "void", "volatile", "while", "bool",
"catch", "class", "const_cast", "delete", "dynamic_cast",
"explicit", "false", "friend", "inline", "mutable", "namespace",
"new", "operator", "private", "protected", "public",
"reinterpret_cast", "static_cast", "template", "this", "throw",
"true", "try", "typeid", "typename", "using", "virtual", "include" };

// read in a C++ program from a cpp file, and put it to the array "program".
void load(char program[][200], int &numLines){
	
	ifstream infile;
	infile.open("Source.cpp");
	int i = 0;
	while (infile >> numLines){
		program[i][200] = numLines;
		
	}
	
		infile.close();
}

void deleteComments(char line[]){
	
	int i = 0;

	while (line[i] != '\0' && (line[i] != '/' || line[i + 1] != '/'))
		i++;

	if (line[i] == '/' && line[i + 1] == '/')
	{
		line[i] = '\0';
		return;
	}

}// if there is a single-line comment in "line", delete it.

void deleteStringConstants(char line[]){
	int b = 0;
	int e;
	while (line[b] != '\0')
	{
		while (line[b] != '\0' && line[b] != '"')
			b++;
		if (line[b] == '\0')
			break;
		e = b + 1;
		if (line[e] == '\0')
			break;
		while (line[e] != '\0' && (line[e - 1] == '\\' || line[e] != '"'))
			e++;
		if (line[e] == '\0')
			break;
		for (int i = b; i <= e; i++)
			line[i] = ' ';
		b = e + 1;
	}
}
// if there are string constants in "line", delete them.

void deleteCharacterConstants(char line[]){
	
	int i;
	int len = strlen(line);
	int rem = 1;

	
	for (i = rem; i < len - 1; i++) line[i] = line[i + 1];
	if (i < len) line[i] = '\0';

}// if there are character constants in "line", delete them.

// put all identifiers in "line" into identifiers[ numIdentifiers ].
void getIdentifiers(char line[], char identifiers[][32], int &numIdentifiers){
	int e = 0;
	for (int i = 0; i <= 32; i++)
		identifiers[e][32] = line[i];
		
		
}
	





// if character is a letter, digit or underscore, then return true, otherwise return false.
bool legal(char character){
	int i = 0;
	
	if ((character == 95) || (character == 48 - 57) || (character == 65 - 90 || character == 97 - 122))
		return 1;
	else
		return 0;
	
		
	
}

// print all non-number, non-keyword strings in "identifiers" into a text file.
void store(char identifiers[][32], int numIdentifiers){

	ofstream out("a.txt", ios::out);
	if (!out)
	{
		cerr << "File could not be opened." << endl;
		exit(1);
	}
	identifiers[][32]=a
	out << a << endl;
	
	out.close();
}

bool isNumber(char string[]){
	int i = 0;

	while (string!="/0")
	if (string[i] == 65 - 90)
		return 1;
	return 0;
		
}
// if "string" consists of digits only, then return true, otherwise return false.

bool isKeywords(char string[]){
	if (string[] == keywords[])
		return 1;
		
	return 0;

}// if "string" is a keyword of C++, then return true, otherwise return false.

// if there is a nonnegtive integer i < pos such that identifiers[ pos ] is equal to identifiers[i],
// then return true; otherwise return false.
bool isDuplicate( char identifiers[][ 32 ], int pos );



int main()
{
	

	
	char program[ 1000 ][ 200 ];
   int numLines = 0;

   load( program, numLines ); // reads in a C++ program from a cpp file, and put it to the array program.

   char identifiers[ 2000 ][ 32 ];
   int numIdentifiers = 0;

   for( int i = 0; i < numLines; i++ )
   {
      deleteComments( program[ i ] ); // if there is a single-line comment in program[ i ], delete it.
      deleteStringConstants( program[ i ] ); // if there are string constants in program[ i ], delete them.
      deleteCharacterConstants( program[ i ] ); // if there are character constants in program[ i ], delete them.
      if( strcmp( program[ i ], "" ) != 0 )
         
         getIdentifiers( program[ i ], identifiers, numIdentifiers );
   }

   // print all non-number, non-keyword strings in "identifiers" into a text file.
   store( identifiers, numIdentifiers );

	system( "pause" );
}
Topic archived. No new replies allowed.