【发布时间】:2013-12-09 06:51:55
【问题描述】:
我有一个非常简单的代码:
void LookupName( string tname ){ //Checks for name in records.txt and enters it if it does not exist
//Variables
string fname; //Used to store the name
string throwaway; //Used to dispose of extra data
bool found = false; //Used to track if a name is found in the file
//objects
fstream file ( STATS_FILE ); //Open the file
if (file.is_open()) {
while ( !file.eof() ) {
getline (file, fname, DELIM); //Fill name
getline (file, throwaway, '\n'); //throw away rest of line
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
if ( fname == tname ) { //Otherwise, continue
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
found = true;
}
}
if ( found == false ) { //if the name is not found
//Reopen the file so that we can write to it
file.close();
file.open( STATS_FILE, fstream::in | fstream::out | fstream::app );
cout << "Not found" <<endl;
Pause();
file << tname << ",0,0\n"; //Add it to the file with 0 wins and losses
}
//Cleanup
file.close();
}
}
这可行,但是如果您在我检查是否找到名称时注意到底部,我必须关闭并重新打开文件。
以下由于某种原因无法正常工作:
void LookupName( string tname ){ //Checks for name in records.txt and enters it if it does not exist
//Variables
string fname; //Used to store the name
string throwaway; //Used to dispose of extra data
bool found = false; //Used to track if a name is found in the file
//objects
fstream file ( STATS_FILE, fstream::in | fstream::out | fstream::app ); //Open the file
if (file.is_open()) {
while ( !file.eof() ) {
getline (file, fname, DELIM); //Fill name
getline (file, throwaway, '\n'); //throw away rest of line
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
if ( fname == tname ) { //Otherwise, continue
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
found = true;
}
}
if ( found == false ) { //if the name is not found
cout << "Not found" <<endl;
Pause();
file << tname << ",0,0\n"; //Add it to the file with 0 wins and losses
}
//Cleanup
file.close();
}
}
我很想知道为什么它在第二个示例中不起作用,因为使用正确的标志仅打开文件 1 次似乎更有效,做我需要做的事情并关闭它。
我感觉这可能与光标的位置有关,我曾尝试使用类似 file.seekg(0) 和 file.seekg(0, ios_base::beg) 但它们没有似乎像宣传的那样工作(或者我只是误解了广告)。
我们将不胜感激。
编辑:couts 用于调试。
编辑 2:我想我应该再强调一下这个问题。
问题是第二个示例没有像第一个示例那样写入文件。我知道对 !file.eof() 条件可能会有一些担忧,但在这种情况下,我不在乎它是否运行额外的时间,因为它不会对结果产生负面影响(此外,正在读取的文本文件已正确格式化,因此不会发生这种情况)。
编辑 3: 我创建了一个运行的非常小的程序:
//Testing bs
fstream file("Test.txt", fstream::in | fstream:: out | fstream::app );
string temp;
//ClearScreen
system(CLEAR_SCREEN);
file << "Line one\n";
getline(file, temp);
file << temp;
file << "Line two\n";
Pause();
return Menu;
只有第一行被写入文件。我打赌 getline 正在改变流的模式,这就是为什么它是不可写的后记。
最终编辑: 经过大量研究,在上述情况下,重新打开文件是最好的解决方案。最终,问题在于 getline() 与 file.getline() 的使用。我必须重写程序的 1000 行中的太多内容才能“正确”完成。这个故事所讲的道德?如果您遇到此问题,请花一些时间研究istream::getline 和getline(string) 之间的区别,并学会确定何时使用哪个,这样您就不会陷入这种情况。幸运的是,我现在不必修复它,但将来可能会为其他人修复。
【问题讨论】:
-
@TonyD 大量有用的信息,谢谢;但是,这与问题完全无关。我不会标记你的评论,但我也不会投票,不管它值多少钱。
-
改为以二进制模式打开文件,然后 seekg 将“像宣传的那样”工作
-
@claptrap 我相信你是对的;但是,这似乎不是解决整体问题的方法。谢谢你的提示。
-
@Toby - 使用像
while ( !file.eof() )这样的损坏代码,在修复之前,您不能指望其余代码正常工作...
标签: c++