一次读取文件全部内容
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main() {
ifstream in;
in.open("test.txt");
if(!in.is_open()) {
cout << "File can not opened" << endl;
}
// 一次读取文件全部内容
stringstream buffer;
buffer << in.rdbuf();
string content = string(buffer.str());
cout << content << endl;
}
该代码示例展示了如何使用C++通过`stringstream`和`ifstream`一次性读取整个文件的内容,并将其存储为字符串。首先打开文件,然后利用`rdbuf()`方法将文件流缓冲区的内容读入`stringstream`,最后转换成`string`类型输出。
1202

被折叠的 条评论
为什么被折叠?



