首页 > 编程语言 > 详细

C++ 读写文件流

时间:2015-06-11 22:52:06      阅读:251      评论:0      收藏:0      [点我收藏+]
1. 读文件流 

string readpro(const char* path) {

  ifstream infile(path);
  char buf[1024];
  string message = "";

  // 假如 infile 后面没有路径,要先打开文件 infile.open(path);

  if (infile.is_open()) {
    while (infile.good() && !infile.eof()) {
      memset(buf, 0, 1024);
      infile.getline(buf, 1024);
      message += buf;
    }
    infile.close();
  } else
    cout << "error!" << endl;
  return message;
}

 

2. 写文件流:

ofstream outfile("/data/name.out", ios::trunc);
outfile.write((char*)result.string(), result.length());
outfile.close();
 

 

C++ 读写文件流

原文:http://www.cnblogs.com/wxmdevelop/p/4570163.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!