字符串写入文件 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. }