用以下的程序,实现从一个文件中读出数据,并按照每行首字母进行排序后写入另外一个文件中:

#include <cstdlib>

#include <iostream>

#include <fstream>

#include <iterator>

#include <vector>

using namespace std;

int main(int argc, char *argv[])

{

string from , to;

cin>>from>>to; //get source and target file names

ifstream is( from.c_str() );

istream_iterator<string> ii( is );

istream_iterator<string> eos;

vector<string> b( ii, eos );

sort( b.begin() , b.end() );

ofstream os( to.c_str() );

ostream_iterator<string> oo( os , "\n" );

unique_copy( b.begin() , b.end() , oo );

return !is.eof()&& !os;

}

【效果】

1. 源文件的内容为:

一个文件间输入输出数据的例子

2. 执行程序,并输入源文件名和目标文件名(源文件名对应的文件一定要存在,目标文件对应的不必存在,会自动创建)

一个文件间输入输出数据的例子

3. 会自动产生e:\b.txt,内容为:

一个文件间输入输出数据的例子

相关文章:

  • 2021-08-18
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2021-04-29
猜你喜欢
  • 2021-06-20
  • 2021-06-20
  • 2021-08-10
  • 2021-07-17
  • 2022-12-23
  • 2021-10-06
  • 2022-02-02
相关资源
相似解决方案