我有以下代碼,到目前爲止,我想檢查文件名是否已經在鏈接列表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!
}
您是否嘗試過使用調試器逐步執行代碼?它應該比使用'print'語句顯示哪個語句更改您的數據更成功。 – 2009-10-11 18:28:04
您能否爲我們提供編譯此文件所需的文件? – 2009-10-11 18:39:44
我建議也使用std :: set來維護文件名而不是自制的列表。 – dimba 2009-10-11 19:29:53