I have the following code, so far, I want to check if a file name is already in the linked list fileList
(or flist
). according to the output, the string saved in the first Node was changed somewhere in Node* getFileName(Node *&flist)
How did this happen? Also, is there anything else that I'm doing is wrong or not safe regarding pointers of Node and strings?
output:
in main: file4.txt start of process: file4.txt file4.txt mid of process: file4.txt" in contains, fileName in node: file4.txt" in contains, target file name: file4.txt end of process: file4.txt" 0 no recursive call
code:
struct Node {
string fileName;
Node *link;
};
/*
*
*/
bool contains (Node *&flist, string &name) {
Node *tempNode = *&flist;
while (tempNode != 0) {
cout << "in contains, fileName in node: " << flist->fileName << endl;
cout << "in contains, target file name: " << name << endl;
if ((tempNode->fileName) == name) {
return true;
}
else {
tempNode = tempNode->link;
}
}
return false;
}
/*
*
*/
Node* getLastNode (Node *&flist) {
Node *tempNode = *&flist;
while (tempNode != 0) {
tempNode = tempNode->link;
}
return tempNode;
}
/*
*
*/
string getFileName(string oneLine) {
char doubleQuote;
doubleQuote = oneLine[9];
if (doubleQuote == '\"') {
string sub = oneLine.substr(10); //getting the file name
string::size_type n = sub.size();
sub = sub.substr(0,n-1);
cout << sub << endl;
return sub;
}
return NULL;
}
/*
*
*/
void process( istream &in, ostream &out, Node *&flist ) {
cout << "start of process: " << flist->fileName << endl;
string oneLine; //temp line holder
while (getline(in, oneLine)) {
// cout << oneLine << endl;
string::size_type loc = oneLine.find("#include",0);
if (loc != string::npos) {
//found one line starting with "#include"
string name;
name = getFileName(oneLine);
cout << "mid of process: " << flist->fileName << endl;
bool recursive;
recursive = contains(flist, name);
cout << "end of process: " << flist->fileName << endl;
cout << recursive << endl;
if (recursive) {
//contains recursive include
cerr << "recursive include of file " << name << endl;
exit(-1);
}
else {
//valid include
cout << "no recursive call" << endl;
}//else
}//if
}//while
}//process
/*
*
*/
int main( int argc, char *argv[] ) {
istream *infile = &cin; // default value
ostream *outfile = &cout; // default value
Node* fileList;
switch ( argc ) {
case 3:
outfile = new ofstream( argv[2] ); // open the outfile file
if ( outfile->fail() ) {
cerr << "Can't open output file " << argv[2] << endl;
exit( -1 );
}
// FALL THROUGH to handle input file
case 2:
infile = new ifstream( argv[1] ); // open the input file
if ( infile->fail() ) {
cerr << "Can't open input file " << argv[1] << endl;
exit( -1 );
}
else {
Node aFile = {argv[1], 0};
fileList = &aFile;
cout << "in main: " << fileList->fileName << endl;
}
// FALL THROUGH
case 1: // use cin and cout
break;
default: // too many arguments
cerr << "Usage: " << argv[0] << " [ input-file [ output-file ] ]" << endl;
exit( -1 ); // TERMINATE!
}
processOneFile (*infile, *outfile, fileList);
// do something
if ( infile != &cin ) delete infile; // close file, do not delete cin!
if ( outfile != &cout ) delete outfile; // close file, do not delete cout!
}