写数据在txt文件中,并计算数据的和
【问题描述】
在当前目录文件“data.txt”中读取整数数据,并计算总和保存在当前目录的“result.txt”文件中。
【输入形式】
文件中的若干数据,格式为按行存储。
【输出形式】
输出保存的文件,和计算之和
【样例输入】
【样例输出】
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin("data.txt");
ofstream fout("result.txt");
int n,ans=0;
while(fin >> n)
{
ans += n;
}
fout << ans;
fin.close();
fout.close();
return 0;
}