字符串写入文件 C++ 读文件 将文件内容读入到字符串string中

 字符串写入文件   :https://zhidao.baidu.com/question/558706893.html

示例:#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
// istream fin;
fout.open("c:\\1.txt");
fout<<"123"<<endl;
fout.close();
return 0;
}

http://blog.csdn.net/my_xxh/article/details/51278440

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <string>  
  2. #include <fstream>  
  3. #include <sstream>  
  4. #include <iostream>  
  5.   
  6. #include <stdlib.h>  
  7. using namespace std;  
  8.   
  9.   
  10. //从文件读入到string里  
  11. string readFileIntoString(char * filename)  
  12. {  
  13. ifstream ifile(filename);  
  14. //将文件读入到ostringstream对象buf中  
  15. ostringstream buf;  
  16. char ch;  
  17. while(buf&&ifile.get(ch))  
  18. buf.put(ch);  
  19. //返回与流对象buf关联的字符串  
  20. return buf.str();  
  21. }  
  22.   
  23.   
  24. int main()  
  25. {  
  26. //文件名  
  27. char * fn="a.txt";  
  28. string str;  
  29. str=readFileIntoString(fn);  
  30. cout<<str<<endl;  
  31. system("pause");  
  32.   
  33. }  

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值