读书人

C++ Again(一):文件读入与写出

发布时间: 2013-11-01 14:43:02 作者: rapoo

C++ Again(1):文件读入与写出

本文章的实现参考自<C++ Primer>第一章第5节。

当前的任务是实现一个C++程序,能够从某个文件读入字符串并将字符串写入到另一个文件中。

实现代码如下:

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){ // ofstream outfile("out_file"); // ifstream infile("in_file");ofstream outfile;ifstream infile;outfile.open("out_file",ios::app);infile.open("in_file");  if(! infile){      cerr<<"error:unable to open file"<<endl;  return -1;  }  if(! outfile){      cerr<<"error:unable to open outfile"<<endl;  return -2;  }  string word;  while(infile >> word)  outfile << word << '~';  return 0;}

读书人网 >网络基础

热点推荐