我目前有一个小程序,它会将.txt文件的内容重写为字符串。收集.txt文件的内容作为字符串,C++
但是,我想收集文件的所有内容作为一个单一的字符串,我该怎么做呢?
#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;
}
}
http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring – 2010-11-03 07:26:02