Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 412349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:04:35+00:00 2026-05-12T18:04:35+00:00

I have the following code, so far, I want to check if a file

  • 0

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!
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:04:35+00:00Added an answer on May 12, 2026 at 6:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to what I have found so far, I can use the following code:
I want to turn off disks (WMI). So far, I have the following code:
I have following code: <div class='parent'> <div class='left-child'></div> <div class=right-child></div> </div> What I want
I have following code class User attr_accessor :name end u = User.new u.name =
I want to write a batch file that performs the following operations: Check if
So far i have the following code, but it doesn't seem to be working,
I have the following code. I want to add two input fields together and
I have the following code, I have implemented something like this so far, but
I have the following code generated by Eclipse (.java file). import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Display;
I have the following code which I want to use to make sure that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.