为什么这个fstream命令不起作用?

时间:2014-04-03 13:48:21

标签: c++ fstream

这是代码。

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, const char * argv[])
{
    ifstream myfile;
    myfile.open("numbers.txt");

    if (myfile.is_open()){
        cout <<" okay to proceed" << endl;
    } else {
        cout<< "error finding file" <<endl;
    }
}

该文件与程序名称完全相同,位于同一文件夹中。

我做错了什么? is_open()检查失败&gt; _&lt;

编辑:已解决。找到product - scheme - options

下的工作目录

1 个答案:

答案 0 :(得分:2)

很多原因: 1.没有r权限(无法访问读取文件) 2.“numbers.txt”位于其他目录中,而不是一个应用程序已启动 ... 在myfile.open中使用完整路径(“FULLPATH / numbers.txt”);只是为了确保你打开正确的文件。 比检查访问权限(依赖于操作系统)

相关问题