Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this string changed?
    primarykey
    data
    text
    <p>I have the following code, so far, I want to check if a file name is already in the linked list <code>fileList</code> (or <code>flist</code>). according to the output, the string saved in the first Node was changed somewhere in <code>Node* getFileName(Node *&amp;flist)</code> How did this happen? Also, is there anything else that I'm doing is wrong or not safe regarding pointers of Node and strings?</p> <p>output:</p> <pre> 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 </pre> <p>code:</p> <pre><code>struct Node { string fileName; Node *link; }; /* * */ bool contains (Node *&amp;flist, string &amp;name) { Node *tempNode = *&amp;flist; while (tempNode != 0) { cout &lt;&lt; "in contains, fileName in node: " &lt;&lt; flist-&gt;fileName &lt;&lt; endl; cout &lt;&lt; "in contains, target file name: " &lt;&lt; name &lt;&lt; endl; if ((tempNode-&gt;fileName) == name) { return true; } else { tempNode = tempNode-&gt;link; } } return false; } /* * */ Node* getLastNode (Node *&amp;flist) { Node *tempNode = *&amp;flist; while (tempNode != 0) { tempNode = tempNode-&gt;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 &lt;&lt; sub &lt;&lt; endl; return sub; } return NULL; } /* * */ void process( istream &amp;in, ostream &amp;out, Node *&amp;flist ) { cout &lt;&lt; "start of process: " &lt;&lt; flist-&gt;fileName &lt;&lt; endl; string oneLine; //temp line holder while (getline(in, oneLine)) { // cout &lt;&lt; oneLine &lt;&lt; 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 &lt;&lt; "mid of process: " &lt;&lt; flist-&gt;fileName &lt;&lt; endl; bool recursive; recursive = contains(flist, name); cout &lt;&lt; "end of process: " &lt;&lt; flist-&gt;fileName &lt;&lt; endl; cout &lt;&lt; recursive &lt;&lt; endl; if (recursive) { //contains recursive include cerr &lt;&lt; "recursive include of file " &lt;&lt; name &lt;&lt; endl; exit(-1); } else { //valid include cout &lt;&lt; "no recursive call" &lt;&lt; endl; }//else }//if }//while }//process /* * */ int main( int argc, char *argv[] ) { istream *infile = &amp;cin; // default value ostream *outfile = &amp;cout; // default value Node* fileList; switch ( argc ) { case 3: outfile = new ofstream( argv[2] ); // open the outfile file if ( outfile-&gt;fail() ) { cerr &lt;&lt; "Can't open output file " &lt;&lt; argv[2] &lt;&lt; endl; exit( -1 ); } // FALL THROUGH to handle input file case 2: infile = new ifstream( argv[1] ); // open the input file if ( infile-&gt;fail() ) { cerr &lt;&lt; "Can't open input file " &lt;&lt; argv[1] &lt;&lt; endl; exit( -1 ); } else { Node aFile = {argv[1], 0}; fileList = &amp;aFile; cout &lt;&lt; "in main: " &lt;&lt; fileList-&gt;fileName &lt;&lt; endl; } // FALL THROUGH case 1: // use cin and cout break; default: // too many arguments cerr &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " [ input-file [ output-file ] ]" &lt;&lt; endl; exit( -1 ); // TERMINATE! } processOneFile (*infile, *outfile, fileList); // do something if ( infile != &amp;cin ) delete infile; // close file, do not delete cin! if ( outfile != &amp;cout ) delete outfile; // close file, do not delete cout! } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload