Почему эта строка изменилась? - PullRequest
0 голосов
/ 11 октября 2009

У меня есть следующий код, поэтому я хочу проверить, есть ли уже имя файла в связанном списке fileList (или flist). согласно выводу строка, сохраненная в первом узле, была изменена где-то в Node* getFileName(Node *&flist) Как это произошло? Кроме того, есть ли что-то еще, что я делаю неправильно или небезопасно в отношении указателей узла и строк?

выход:

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

код:

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!
}

Ответы [ 3 ]

2 голосов
/ 11 октября 2009

Не могли бы вы опубликовать оригинальный код? Размещенный вами код даже не компилируется.

Ошибки, которые я заметил, по порядку:

processOneFile (*infile, *outfile, fileList);

Нет процедуры processOneFile().

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

Это вызовет processOneFile() с неинициализированным списком файлов, который вылетит при попытке напечатать имя файла.

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

aFile находится только в области действия в этом else, поэтому попытка использовать указатель на него позже не удастся.

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

Вы не можете создать std::string из NULL - это приведет к сбою программы.

После исправления этих ошибок, чтобы ваш код не зависал, я не смог воспроизвести ошибку.

Если вы работаете в Linux, попробуйте увеличить уровень предупреждения (с g++ -Wall -Wextra -ansi -pedantic) и запустить код через valgrind, чтобы проверить ошибки памяти.

0 голосов
/ 11 октября 2009

Кроме того, почему вы тратите время на написание собственного связанного списка, когда стандартная библиотека уже имеет очень хороший список?

0 голосов
/ 11 октября 2009

Хорошо, теперь код выглядит так, как будто он работает, как и ожидалось:

#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!
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...