为什么这个字符串改变了?

时间:2009-10-11 18:14:07

标签: c++ string pointers linked-list

我有以下代码,到目前为止,我想检查文件名是否已经在链接列表fileList(或flist)中。根据输出,保存在第一个节点中的字符串在Node* getFileName(Node *&flist)的某处发生了变化。这是怎么发生的?另外,对于Node和字符串的指针,还有其他任何我做错了或不安全吗?

输出:

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 个答案:

答案 0 :(得分:2)

你能发布原始代码吗?您发布的代码甚至无法编译。

我注意到的错误,按顺序:

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运行代码,以检查内存错误。

答案 1 :(得分:0)

好的,代码现在看起来像预期的那样工作:

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

答案 2 :(得分:0)

另外,当标准库已经有一个非常好的链接列表时,你为什么要浪费时间编写自己的链表呢?