我正在尝试使用以下代码打开 /usr/share/dict/words
:
fstream f;
f.open("/usr/share/dict/words");
// why is this returning false?
bool open = f.is_open();
我想知道为什么 f.is_open()
返回 false?
更多信息:当我尝试包含大约 20 行的较小测试文件时,f.is_open()
返回 true。也许 f.open
正在尝试将整个文件加载到内存中?
最佳答案
它不起作用,因为您正在打开文件进行读写。除非您以 root 身份运行,否则您没有写入该文件的权限。
如果你只是为了阅读而打开它,它会起作用:
f.open("/usr/share/dict/words", fstream::in);
关于c++ - fstream open 在处理大文件时有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8041020/