Ifstream无法使用amdinistrator权限打开文件

时间:2017-09-09 16:49:22

标签: c++ file text ifstream

我使用此代码尝试打开并读取文件(非空),但ifstream无法正常工作 - 它无法打开文件:我在文件打开时添加了检查并显示,ifstream甚至没有(不能打开文件。

我为该程序授予了管理员权限,但ifstream仍然无法读取该文件。

我也试图找到一条路径,ifstream会读取此文件,但我没有成功,最后我尝试使用绝对路径打开文件 - 但结果是一样的。

该文件位于程序的根文件夹中,但我将它放在任何地方并且没有任何更改。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
    string s;
    ifstream file("fix.txt");
    if (file)
        cout << "SUCCESSFULL OPENING" << endl;

    while (getline(file, s)) {
        cout << s << endl;
        s += "+";
        cout << s << endl;
    }

    file.close();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您可以通过

激活流上的例外来访问更详细的错误代码
file.exceptions(std::ios_base::failbit);

然后,您可以通过编写

获得更多详细信息
try {
   file.open("fix.txt");
}
catch(std::ios_base::failure& f) {
    // f.what() contains a message, f.code() returns a std::error_code
}
相关问题