用C++读取文件

gpr*_*ime 4 c++

我无法弄清楚为什么我的代码无法打开和读取文件.我错过了什么?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main (int argc, char * const argv[]) 
{
    string line;
    ifstream myfile ("input_file_1.txt");
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            getline (myfile,line);
            cout << line << endl;
        }
    }
    else
    {
        cout << "Was unable to open the file" << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

文件"input_file_1.txt"与我的.cpp文件在同一目录中,并且具有读取权限.我甚至给了它777权限,我无法阅读它.

谁能告诉我我做错了什么?我真的想不出来......

Lio*_*gan 5

  • 尝试使用该文件的完整路径
  • 查找文件的默认位置是可执行文件所在的位置,而不是源位置.