我目前有一个小程序,可以将 .txt 文件的内容重写为字符串。
但是我想将文件的所有内容收集为一个字符串,我该怎么做呢?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string file_name ;
while (1 == 1){
cout << "Input the directory or name of the file you would like to alter:" << endl;
cin >> file_name ;
ofstream myfile ( file_name.c_str() );
if (myfile.is_open())
{
myfile << "123abc";
myfile.close();
}
else cout << "Unable to open file" << endl;
}
}