怎样在c++中读入txt文件
来源:百度知道 编辑:UC知道 时间:2024/09/22 01:02:12
txt文件中共有1000个数,就是把6分成1000份,0.006,0.012,0.018。。。一直到6(数组A[i])。现在要读入后计算得到数组B[i],其中B[i]=pow(2.718,A[i]),也就是说B[i]=exp(A[i])。从txt读入A[i]1000个数据后计算完得到B[i]1000个数据输出,求达人帮忙编个程序。十分感谢。高分答谢(>100分)。急!!!
看一下这样如何。
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
void main()
{
/*
ofstream out("data.txt");
double num = 0.006;
for (int i = 1; i <= 1000; ++i)
{
out << num * i << endl;
}
out.close();
*/
ifstream in("data.txt");
double A[1000], B[1000], temp;
int j, i = 0;
if (!in)
{
cerr << "Can't open the file" << endl;
return;
}
while (1)
{
in >> temp;
if (in.eof()) break;
A[i++] = temp;
}
cout << i << endl;
for (j = 0; j < i; ++j)
{
B[j] = pow(2.718, A[j]);
cout << B[j] << ' ';
}
cout << endl;
in.close();
}