c++ - 无法打开文件

标签 c++ file get fstream

我正在尝试打开文件,一个文本文件。

我已经包含了字符串和 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/

相关文章:

c++ - 纹理不会在 OpenGL 和 Xcode 上显示

c++ - 具有 sudo 权限的 OpenMPI/mpirun 或 mpiexec

java - 如何同时打印到文件和显示器?

javascript - 为什么这个 while 循环不调用其内部函数?

php - 嵌入式 iframe - 验证 GET 请求的来源/来源

c++ - 网络浏览器中定义的 zlib gzip 无效响应 (c++)

android - 从 Uri 打开文件,独立于 android 中的位置

Android:发送要保存在服务器数据库中的文件

php - 单击链接更改语言

C++ std::从 std::deque 复制到 std:;set