C++ 读写文件流

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();
 
原文地址:https://www.cnblogs.com/wxmdevelop/p/4570163.html