您的位置:首页 > 其它

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

2007-08-10 17:34 330 查看
用以下的程序,实现从一个文件中读出数据,并按照每行首字母进行排序后写入另外一个文件中: #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,内容为:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: