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 6022103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:48:38+00:00 2026-05-23T03:48:38+00:00

sorry to post the spaghetti code, but I don’t know where the error is

  • 0

sorry to post the spaghetti code, but I don’t know where the error is coming from. This function is supposed to mimic a simplified version of the file inclusion aspect of c++. It copy pastes the contents of all files with a #include (and recursively on the contents of the included file).

In order to ensure that 2 files don’t recursively include each other, I created a linked list that stores every single file included previously. Files included afterwards will be checked against this linked list to ensure that a file does not include a previous file. We are not allowed to dynamically allocate so we must use memory from the stack for the linked list. I heard that this could cause memory problems with the linked list when variables go out of scope.

The problem is, parts of my linked list are being overwritten by random things. If you note the cout statements, a result could be something like this

Previous flist: input.txt

newnode filename: file1.txt

new flist: file1.txt

Previous flist: WHATEVER DOESNT MATTER (weird, it took on the value of string filename)

newnode filename: file2.txt

new flist: file2.txt

Previous flist: file2.txt (working as intended)

newnode filename: file3.txt

new flist: file3.txt

Previous flist: random symbols (huh? is this from random memory in the stack?)

newnode filename: file4.txt

new flist: file4.txt

Previous flist: file4.txt (working as intended)

newnode filename: file5.txt

new flist: file5.txt

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

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

string extractfilename (string str) 
{
    string filename;

    if ((str.substr(10,1)) == "/" )
    {
        filename = str.substr((str.find_last_of("/"))+1,(str.length()) - (str.find_last_of("/"))-2);
        return filename;            
    } // if     
    else if ( (str.find_last_of("/")) != -1)
    {
        filename = str.substr((str.find_last_of("/")) + 1, (str.length()) - (str.find_last_of("/")) - 2);   
        return filename;
    } // else if 
    else
    {
        filename = str.substr(10,(str.length())-11);
        return filename;
    } // else

    return "ERROR";
}

void check_overlap (string filename, Node *flist)
{  
    while (flist != NULL)
    {
        if (flist->fileName == filename)
        {
            cerr << "Recursive include is being attempted. Terminating program." << endl;
            exit ( -1 );
        }
        flist = flist->link;
    }
}

void processOneFile( istream &in, ostream &out, Node *flist, string prefixDir )
{
    string str;
    getline(in,str);

    while(!(in.fail()))
    {
        string checkinclude = "";
        string checkabsolute = "";
        string prefix = "";
        string filename = "WHATEVER DOESNT MATTER";    
        string relpath = "";

        int checkrelative = 0;    
        int lastof = 0;    
        int length = str.length();

        if ( length > 11)
        {    
            checkinclude = str.substr(0,8);
            checkabsolute = str.substr(10,1);
            checkrelative = str.find_last_of("/");
        }    

        if (checkinclude == "#include")
        {
            ifstream newinput;
            filename = extractfilename(str);

            // PROBLEM WITH THIS ************
            //check_overlap(filename,flist) CAUSES INFINITE LOOP DUE TO DANGLING POINTERS?      
            Node newnode;

            cout << "Previous flist:    "<< flist->fileName << endl;

            newnode.fileName = filename;
            newnode.link = flist;
            Node surrogate_flist = newnode;

            cout << "newnode filename: "<< newnode.fileName << endl;       
            cout << "New flist:         "<< surrogate_flist.fileName << endl;
            cout << endl;
            // PROBLEM WITH THIS **************

            if (checkabsolute == "/" )
            {
                lastof = str.find_last_of("/");
                prefix = str.substr(10,lastof - 9);                    
                newinput.open((prefix + filename).c_str());

                if (!(newinput.is_open()))
                {
                    cout << prefix+filename << " cannot be opened" << endl;
                    exit( -1) ;
                }

                processOneFile(newinput,out,&surrogate_flist,prefix);
                newinput.close();
            } // if        
            else if ( checkrelative != -1)
            {
                relpath = str.substr(10, checkrelative - 9);        
                newinput.open((prefixDir+relpath+filename).c_str());

                if (!(newinput.is_open()))
                {
                    cout << prefixDir + relpath + filename << " cannot be opened" << endl;
                    exit( -1) ;
                }

                processOneFile(newinput,out,&surrogate_flist,(prefixDir+relpath));
                newinput.close();
            } // else if
            else
            {
                newinput.open((prefixDir + filename).c_str());

                if (!(newinput.is_open()))
                {
                    cout << prefixDir +filename << " cannot be opened" << endl;
                    exit( -1) ;
                }

                processOneFile(newinput,out,&surrogate_flist,prefixDir);
                newinput.close();
            } // else
        } // if
        else
        {
            out << str << endl;
        } // else
        getline(in,str);
    } // while
} // processOneFile

Thanks

EDIT: Use of the node structure and linked lists is a requirement

Implicit dynamic allocation from string functions is allowed

added main and “complete” compilable code

  • 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-23T03:48:39+00:00Added an answer on May 23, 2026 at 3:48 am

    Given the requirement to use a linked list, you must use recursion. It would be something like this:

    struct Node
    {
        string value;
        Node *link;
    };
    
    void Process(Node *front)
    {
        Node newNode;
        newNode.link = front;
        if( /* some condition */ )
            return; // end recursion
        else
            Process(&newNode);
    }
    

    The first invocation to Process would simply be Process(NULL); to indicate an empty list. Add additional parameters to include the rest of your state, of course (I’m not here to do your homework for you ;)).

    The important thing is that you do not modify front->link, because your new node will not be valid once the function returns and must therefore not become visible to the caller. So you must build this list in reverse (each recursive call adds a node to the front).

    You are already doing something similar to this, so your problem may lie elsewhere. Without complete, compilable code it’s hard to determine where, however.

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

Sidebar

Related Questions

this post is a long one sorry for that but the problem is complex
First of all, sorry to post a question like this when so many other
Long post.. sorry I've been reading up on this and tried back and forth
This is the first time for me to post here, so sorry if I
(Sorry if this sounds like a rant, but it's a real question and I'd
Sorry, I know that this topic has been covered a bit. I've read the
(Sorry for long post) Ok guys, so I'm having some issues with something I'm
first post here so sorry for the length of it. I've been lurking and
Sorry, I don't have a lot of experience with audio and video on the
Sorry for some crazy subj. I'd like to override django models save method and

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.