c++文件操作 读文件 代码没有报错 但是好像找不到它好像找不到我写的文件

img

img

img

img



#include <vector> 
#include <string> 
#include <fstream> 
#include <iostream> 
 
using namespace std; 
 
int main() 
{ 
    ifstream myfile("G:\\C++ project\\Read\\hello.txt"); 
    ofstream outfile("G:\\C++ project\\Read\\out.txt", ios::app); 
    string temp; 
    if (!myfile.is_open()) 
    { 
        cout << "未成功打开文件" << endl; 
    } 
    while(getline(myfile,temp)) 
    { 
        outfile << temp; 
        outfile << endl;
    } 
    myfile.close(); 
    outfile.close();
    return 0; 
} 

我以前是这样编的,成功了

啊这,应该是代码有误,我看看

没找到文件,文件用全路径试试。

输出了“文件无法打开” 说明进了那个if。

  1. 文件名补充未绝对路径。
  2. ifstream 参数使用 ios::in | ios::binary 试试