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

#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,内容为:

posted on 2007-08-10 17:34  今夜太冷  阅读(336)  评论(0)    收藏  举报