如何将a.txt文件的内容
平均分配到,
b.txt和 c.txt中
1.把a.txt的文件内容放进vector<string> svec中
2.分别读取svec的一半内容放到 b.txt和 c.txt中
[CODE]#include <fstream>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream infile("D:\\a.txt",ios::in);
if(!infile)
{
cout<<"can't open a.txt"<<endl;
exit(-1);
}
vector<string> svec;
string word;
while(infile>>word)
svec.push_back(word);
infile.close();
ofstream outfile1("D:\\b.txt");
if(!outfile1)
{
cout<<"can't open b.txt"<<endl;
exit(-1);
}
vector<string>::const_iterator iter = svec.begin();
int ct = 0;
while(iter != svec.begin()+svec.size()/2)
{
ct++;
if(ct == 15)
outfile1<<endl;
outfile1<<*iter<<' ';
iter++;
}
outfile1.close();
ofstream outfile2("D:\\c.txt");
if(!outfile2)
{
cout<<"can't open c.txt"<<endl;
exit(-1);
}
iter = svec.begin()+svec.size()/2;
ct = 0;
while(iter != svec.end())
{
ct++;
if(ct == 15)
outfile2<<endl;
outfile2<<*iter<<' ';
iter++;
}
outfile2.close();
return 0;
}
[/CODE]