我在读取简单文件时遇到问题。我有 4 行四行长的 T/F,开头有计数。我读入了 myfile >> count 和 myfile >> value 来获取值,并用 count 完成它并转到下一行,但由于某种原因我在转到第三行时遇到了问题。不确定如何在此处获取数据文件...感谢查看!
int main() {
ifstream myfile;
int count;
string value;
myfile.open("branches.txt");
while(!myfile.eof()) {
myfile >> count;
cout << count << endl;
while(count > 0) {
myfile >> value;
count--;
//cout << value;
}
myfile >> count;
}
system("pause");
return 0;
}
最佳答案
不要使用 feof()
它只会告诉您上次读取的结果是什么。读取文件的正确方法是
while( read( file, buffer ) )
{
//do something
}
关于C++读取文件循环搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6894176/