views:

129

answers:

3

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!
}
+2  A: 

Could you post the original code? The code you posted doesn't even compile.

Errors I've noticed, in order:

processOneFile (*infile, *outfile, fileList);

There is no processOneFile() procedure.

istream *infile  = &cin;                            // default value
ostream *outfile = &cout;                           // default value
Node* fileList;
case 1:                                       // use cin and cout
    break;
processOneFile (*infile, *outfile, fileList);

This will call processOneFile() with an uninitialized file list, which will crash when you try to print the file name.

    else {
            Node aFile = {argv[1], 0};
            fileList = &aFile;
            cout << "in main: " << fileList->fileName << endl;
    }

aFile is only in scope within that else, so trying to use a pointer to it later will fail.

string getFileName(string oneLine) {
    ///
    return NULL;
}

You can't construct a std::string from NULL -- this will crash the program.

After fixing these errors so your code wouldn't crash, I couldn't reproduce the error.

If you're building in Linux, try increasing the warning level (with g++ -Wall -Wextra -ansi -pedantic) and running your code through valgrind, to check for memory errors.

John Millikin
Håkon
You can initialize a string from NULL. The string will be empty.
GMan
@GMan: No, initializing a `std::string` from `NULL` is undefined. On my system (and codepad, http://codepad.org/ZldIZHwo), it raises an exception.
John Millikin
A: 

Ok, the code does now seem like it works as expected:

#include <iostream>
#include <fstream>

using namespace::std;

struct Node
{
    string fileName;
    Node *link;
};

bool contains (Node *&flist, string &name)
{
    Node *tempNode = *&flist;
    while (tempNode != 0)
    {
        cout << "Searching in \"" << flist->fileName;
        cout << "\" for \"" << 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);
        return sub;
    }
    return "";
}

void process( istream &in, ostream &out, Node *&flist )
{
    cout << "Start of process: " << flist->fileName << endl << endl;
    string oneLine;                 
    while (1)
    {
     cout << "Input include statement: ";
     getline(in, oneLine);

     if (oneLine == "STOP")
      return;

        string::size_type loc = oneLine.find("#include",0);
        if (loc != string::npos)
     {
      //found one line starting with "#include"
      string name;
      name = getFileName(oneLine);
      if (name == "")
      {
       cout << "Couldn't find filename, skipping line..." << endl;
       continue;
      }

      if (contains(flist, name))
      {
       //contains recursive include
       cerr << "Uh, oh! Recursive include of file " << name << endl;
       exit(-1);
      }
      else
      {
       cerr << "No recursive include" << endl;
      }

        }//if

     cout << endl;
    }//while
}

int main( int argc, char *argv[] )
{
    Node* fileList = new Node;
    istream *infile  = &cin;                            // default value
    ostream *outfile = &cout;                           // default value
    fileList->fileName = "Input";      // default value

    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 {
                fileList->fileName = argv[1];
                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!
    }

    process(*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!
}
Håkon
A: 

Also, why are you wasting time writing your own linked list when the standard library already has a perfectly good one?

Ross Smith
It's a homework assignment, of course.
John Millikin