C++ 为什么要更改此字符串?
我有以下代码,到目前为止,我想检查一个文件名是否已经在链表C++ 为什么要更改此字符串?,c++,string,pointers,linked-list,C++,String,Pointers,Linked List,我有以下代码,到目前为止,我想检查一个文件名是否已经在链表文件列表(或flist)中。根据输出,保存在第一个节点中的字符串在Node*getFileName(Node*&flist)中的某个地方发生了更改,这是如何发生的?另外,关于节点和字符串的指针,我还做了什么错误或不安全的事情吗 输出: in main: file4.txt start of process: file4.txt file4.txt mid of process: file4.txt" in contains, fileNa
文件列表
(或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
主目录:file4.txt
进程开始:file4.txt
file4.txt
进程中间:file4.txt“
在contains中,节点中的文件名:file4.txt“
在contains中,目标文件名:file4.txt
进程结束:file4.txt“
0
无递归调用
代码:
struct节点{
字符串文件名;
节点*链接;
};
/*
*
*/
bool包含(节点*&flist、字符串和名称){
节点*tempNode=*&flist;
while(tempNode!=0){
你能发布原始代码吗?你发布的代码甚至都没有编译
我注意到的错误,顺序如下:
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;
}
不能从NULL
构造std::string
——这将使程序崩溃
在修复了这些错误以使代码不会崩溃后,我无法重现错误
如果您是在Linux中构建,请尝试增加警告级别(使用g++-Wall-Wextra-ansi-pedantic
)并通过valgrind
运行代码,以检查内存错误。好的,代码现在看起来确实像预期的那样工作:
#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!
}
#包括
#包括
使用namespace::std;
结构体类型
{
字符串文件名;
节点*链接;
};
bool包含(节点*&flist、字符串和名称)
{
节点*tempNode=*&flist;
while(tempNode!=0)
{
cout-link;
}
}
返回false;
}
节点*getLastNode(节点*&flist)
{
节点*tempNode=*&flist;
while(tempNode!=0)
{
tempNode=tempNode->link;
}
返回临时节点;
}
字符串getFileName(字符串单行)
{
字符双引号;
双引号=单行[9];
如果(双引号=“\”){
string sub=oneLine.substr(10);//获取文件名
字符串::size_type n=sub.size();
sub=sub.substr(0,n-1);
返回子节点;
}
返回“”;
}
无效流程(istream&in、ostream&out、节点*&flist)
{
还有,既然标准库已经有了一个非常好的链表,你为什么还要浪费时间编写自己的链表呢?你有没有尝试过用调试器单步调试你的代码?它应该比用打印
语句来显示哪条语句更改了你的数据更成功。你能提供给我们文件吗需要编译此文件吗?我建议也使用std::set来维护文件名,而不是自行编制的列表。此外,没有“getFileName(Node*&flist)”这样的函数,这通常会导致问题。只有一个“getFileName(string one line)”,但这无法更改其中一个节点中的字符串。您可以从NULL初始化字符串。字符串将为空。@GMan:否,从NULL
初始化std::string
是未定义的。在我的系统(和代码板,)上,它引发异常。
string getFileName(string oneLine) {
///
return NULL;
}
#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!
}