我从文件中为我的应用程序读取了一些数据,但它最近停止工作了。我觉得它停止工作的时间对应于我从 Notepad++ 切换到 Sublime Text 2 的时间......无论如何,这是我读取数据的代码:
std::ifstream stream;
stream.open("parsing_model.txt");
char ignore_char;
std::string model_class;
int parsing_model;
while (stream >> model_class >> ignore_char >> parsing_model)
{
// snip
// doesn't even make it into a single run of this while loop.
}
我的数据组织为
Item1, 12
Item2, 4
foo, 42
bar, 1
它是文本编码中的东西吗?我怎样才能使我的代码对此健壮并解决我的问题?直到最近,这段代码绝对有效了几个月。谢谢
最佳答案
在使用前检查流是否处于良好状态。
stream.open("parsing_model.txt");
if (stream.good()) {
//... read the stream
} else {
std::cerr << "failed to open input file\n";
}
如果出现故障,请确保当前工作目录与保存输入文件的位置相同。看来你是在windows上运行,所以你应该可以使用这个命令来查看你当前的目录。
system("dir & pause");
关于c++ - std::ifstream 在将文本编辑器从 notepad++ 切换到 sublime text 2 以使用它正在读入的文件后没有读入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11922847/