我正在尝试读取路径中包含西里尔字符的文件,并得到 ifstream.is_open() == false
这是我的代码:
std::string ReadFile(const std::string &path) {
std::string newLine, fileContent;
std::ifstream in(path.c_str(), std::ios::in);
if (!in.is_open()) {
return std::string("isn't opened");
}
while (in.good()) {
getline(in, newLine);
fileContent += newLine;
}
in.close();
return fileContent;
}
int main() {
std::string path = "C:\\test\\документ.txt";
std::string content = ReadFile(path);
std::cout << content << std::endl;
return 0;
}
指定文件存在
我试图在谷歌中找到解决方案,但我一无所获
这是我看到的链接:
附言我需要在字符串中获取文件的内容,而不是在 wstring 中
这是我的 IDE (CLION 2017.1) 的编码设置
最佳答案
您需要一个最新的编译器或 Boost。 std::filesystem::path
可以处理这些名称,但它是 C++17 标准中的新内容。您的编译器可能仍将其作为 std::experimental::filesystem::path
,否则您将使用第三方 boost::filesystem::path
。这些界面非常相似,因为 Boost 版本是灵感来源。
关于c++ - 无法在 C++ 中读取带有西里尔路径的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43405956/