为啥要更改此字符串?

Posted

技术标签:

【中文标题】为啥要更改此字符串?【英文标题】:Why is this string changed?为什么要更改此字符串? 【发布时间】:2009-10-11 18:14:07 【问题描述】:

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

输出:

在主要:file4.txt 进程开始:file4.txt 文件4.txt 进程中间:file4.txt" 包含,节点中的文件名:file4.txt" 在包含,目标文件名:file4.txt 进程结束:file4.txt" 0 没有递归调用

代码:

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!

【问题讨论】:

您是否尝试过使用调试器单步执行您的代码?它应该比使用print 语句来显示哪个语句更改了您的数据更成功。 能否请您提供编译此文件所需的文件? 我还建议使用 std::set 来维护文件名而不是自制列表。 【参考方案1】:

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

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

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;

您不能从NULL 构造std::string——这会使程序崩溃。

修复这些错误以使您的代码不会崩溃后,我无法重现该错误。

如果您在 Linux 中构建,请尝试提高警告级别(使用g++ -Wall -Wextra -ansi -pedantic)并通过valgrind 运行您的代码,以检查内存错误。

【讨论】:

另外,没有所谓的“getFileName(Node *&flist)”这样的函数可能会导致问题。只有一个“getFileName(string oneLine)”,但这不可能改变其中一个节点中的字符串。 你可以从NULL初始化一个字符串。该字符串将为空。 @GMan:不,从 NULL 初始化 std::string 是未定义的。在我的系统(和键盘,codepad.org/ZldIZHwo)上,它引发了一个异常。【参考方案2】:

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

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

【讨论】:

【参考方案3】:

另外,既然标准库已经有一个完美的链表,为什么还要浪费时间编写自己的链表?

【讨论】:

以上是关于为啥要更改此字符串?的主要内容,如果未能解决你的问题,请参考以下文章

为啥要更改此实例对象?

为啥要修改数据库字符集

为啥要使用 CComBSTR 而不是只传递 WCHAR*?

为啥我更改的字符串没有出现在使用选项卡式表单 xamarin 的其他页面上

为啥我在尝试计算字符串字母的程序中出现此错误?

为啥此查询字符串在 Access 中有效,但在 C# 中无效?