C++将文件内容一次性读入内存

来源:互联网 发布:湖南广电网络客服电话 编辑:程序博客网 时间:2024/04/30 00:31

结合字符串流,将文件中的内容一次性读入内存,代码如下:

#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();}