程序应该从data.txt文件读取数字然后打印它们,但是只打印第一个值好,所有其他值都相同-9.25596e + 061
const char CDfv[] = "Data.txt";
const int cmax = 1000;
//---------------------------------------------------------
void read_print_data (double A[], int& x);
//--------------------------------------------------------
int main()
{
ofstream fr;
double A[cmax];
int x;
ifstream fv ("Data.txt");
read_print_data(A,x);
system("Pause");
return 0;
}
void read_print_data (double A[], int& x)
{
ifstream fd(CDfv);
fd >> x;
for (int i = 0; i < x; i++)
{
fd >> A[i];
cout << i + 1 << " " << A[i] << endl;
fd.close();
}
}
答案 0 :(得分:2)
您过早关闭了流,您需要移动
fd.close();
在循环之外。
所以代码看起来应该是
for (int i = 0; i < x; i++)
{
if (fd >> A[i]) {
cout << i + 1 << " " << A[i] << endl;
} else {
// Error has occurred
}
}
fd.close();