【发布时间】:2015-12-09 12:01:32
【问题描述】:
#include <fstream>
#include <iostream>
//Bear in mind that the text file is already in the resources file
int main()
{
ifstream file("Hamlet.txt", ios::in);//open file
if (file.is_open() == true) cout << "File is open" << endl;
else if (file.is_open() == false) cout << "File isnt open" << endl;
return 0;
}
所以我试图查看文件是否打开,文本文件在资源文件中,据我所知文件的路径可以写为 "Hamlet.txt" 。文件一直打不开,请问是什么原因? 任何人都可以对此做出解释吗? 提前致谢
【问题讨论】:
-
是你运行可执行文件的目录(即工作目录)中的txt文件吗?
-
stackoverflow.com/questions/24097580/… 与检查相关,您可能希望使用experimental::filesystem::exists({"Hamlet.txt"}) 来检查路径是否存在(或boost::filesystem,如果你'不在最新的 msvc 上)。
-
你说的是Win32资源文件吗?
-
ifstream file("Hamlet.txt", ios::in);将打开一个名为Hamlet.txt的文件,该文件直接存在于当前工作中(仅此而已)。您提到“资源”,那么您在说什么资源?
标签: c++