<分区>
fstream 的 open() 是采用相对文件路径还是绝对文件路径?这是示例代码 -
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
fstream myfile;
myfile.open("subd/example.txt");
if(myfile.is_open()) {
for(int i = 0; i < 10; i++) {
myfile << "Example data " << i << endl;
}
} else cout << "Unable to open " << endl;
myfile.close();
return 0;
}
我在 MAC OS X 环境中工作。子目录已经存在。 .open() 对每种情况都返回 false。我也在使用 -std=c++11。我该如何解决这个问题? “subd”目录的权限是:
输出总是“无法打开”。 原始帖子(此帖子被标记为重复的帖子)的解决方案没有解决我的问题。
Solution, because none of the responses below completely resolve my issue
Use ofstream with the relative path(with respect to the location of the program executable ie. argv[0]). fstream just doesn't seem to work the same way.