第1个回答 2013-05-30
#include<fstream>
#include<string>
#include<iostream>
using namespace std;
void main()
{
int thesum = 0;
ifstream in("thenum.txt");
string str;
while(getline(in,str)){
int num=0;
stringstream str;
str>>num;
thesum=thesum+num;
}
in.close();
ofstream out;
out.open("thesum.txt",ios::trunc);
out<<thesum;
out.close();
return 0;
}
第2个回答 2013-05-30
这样做:
int a[100],count = 0;
if(stream fin("data.txt");
if(!fin){
exit(-1);
}
while(fin>>a[count++]);
fin.close();
//...处理数据
ofstream fout("result.txt");
if(!fout){
exit(-1);
}
for(int i = 0;i < count;++i)fout<<data[i]<<endl;
fout.close();本回答被网友采纳