我目前有一个小程序可以将 .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;
}
}
最佳答案
#include <sstream>
#include <string>
std::string read_whole_damn_thing(std::istream & is)
{
std::ostringstream oss;
oss << is.rdbuf();
return oss.str();
}
关于c++ - 将 .txt 文件的内容收集为字符串,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4084751/