C++读取文件到string中

ifstrean读取文件

#include
using namespace std; 
 
int main() 
{ 
    ifstream myfile("D:\\VScodeC++\\file\\hello.txt",ios::in); 
    string temp; 
    if (!myfile.is_open()) 
    { 
        cout << "未成功打开文件" << endl; 
    }
    string res;
    while(getline(myfile,temp)){
        res+=temp;
        res+='\n';
    }
    cout<<res;
    myfile.close(); 
    system("pause");
    return 0; 
} 

按原格式输出文件内容

你可能感兴趣的:(C++,file,c++)