结合字符串流,将文件中的内容一次性读入内存,代码如下:
#include <string>
using std::ostringstream;
using std::ifstream;
using std::string;
std::string fileContent;
string strFileName="ServiceIpConfig.txt";//文件名字
fin.open(strFileName.c_str());
if (fin.is_open())
{
ostringstream temp;//字符串流,提供对于string对象的写的功能。
temp<<fin.rdbuf();//往字符串流中写
fileContent= temp.str();
fin.close();
}
本文介绍了一种利用C++中的字符串流一次性将文件内容读入内存的方法。通过使用ostringstream和ifstream,可以方便地实现文件内容的读取,并存储到string变量中。
5488





