c++ - std::ifstream 在将文本编辑器从 notepad++ 切换到 sublime text 2 以使用它正在读入的文件后没有读入?

标签 c++ debugging file-io

我从文件中为我的应用程序读取了一些数据,但它最近停止工作了。我觉得它停止工作的时间对应于我从 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/

相关文章:

c++ - 如果编译后的源代码特定于编译时所使用的硬件,我们该如何分发?

java - 用于打印 javax.servlet.http.Cookie 所有字段的任何实用程序

c# - 我第二次在 HttpPost 中使用 "getResponse()",它只能逐步调试

文件长度的计算不起作用

c - 文件不会逐字节完全读取

C++/Eclipse cdt,避免实现相同功能但签名不同

c++ - 我的哈希表比二进制搜索慢

c++ - 什么是构造函数在c++中的实际应用

java - 在 IntelliJ 中调试(重新加载更改的类)

c - 使用 "fread"获得意外输出