C++读文件将文件内容读入到字符串string中的方法

如下所示:

网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序制作、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了西塞山免费建站欢迎大家使用!

#include 
#include 
#include 
#include 
 
#include 
using namespace std;
 
 
//从文件读入到string里
string readFileIntoString(char * filename)
{
ifstream ifile(filename);
//将文件读入到ostringstream对象buf中
ostringstream buf;
char ch;
while(buf&&ifile.get(ch))
buf.put(ch);
//返回与流对象buf关联的字符串
return buf.str();
}
 
int main()
{
//文件名
char * fn="a.txt";
string str;
str=readFileIntoString(fn);
cout<

以上这篇C++ 读文件 将文件内容读入到字符串string中的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持创新互联。


网页名称:C++读文件将文件内容读入到字符串string中的方法
网站地址:http://csdahua.cn/article/poipdg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流