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

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

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

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

[cpp]  view plain  copy
 print ?
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.   
  6. #include   
  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<
  31. system("pause");  
  32.   
  33. }  

你可能感兴趣的:(c/c++)