我正在尝试打开文件,一个文本文件。
我已经包含了字符串和 fstream。
#include <string>
#include <fstream>
这是我程序中的代码,但每次运行该程序时,它都会显示“无法打开文件”。
float unscreen()
{
cout << "Welcome to the screen help area" << endl;
cout << "\n" << endl;
string line;
ifstream myfile("unsreen.txt");
if(myfile.is_open())
{
while(getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
else cout << "unable to open file";
cout << "\n" << endl;
cout << "Press 0 for further action or press 9 to exit." << endl;
我需要更改文件中的某些内容吗,我在目录中找到了文件。
最佳答案
确保在 projects\projectname\projectname 中有 unsreen.txt
同样的代码工作正常。如果它仍然不适合你然后删除文件评论所有这些代码并添加
ofstream myfile("unsreen.txt");
myfile<<"sdafe";
myfile.close();
然后打开文件,添加你想添加的内容,然后删除这段代码并替换为旧的。
关于c++ - 无法打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21418154/