我正在编写一个程序,需要从文本文件中读取11,000个数字的列表,然后将它们输出到控制台。但是,每当我运行我的代码时,我都能够确定它只打印最后299个数字。这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double dataVector[11000]; //text file has 11,000 elements
string userfile; //name of file selected by user
//prompt user for file to be opened
cout << "Enter the name of the file you would like to read :: ";
cin >> userfile;
ifstream ifs(userfile); //open file
if (!ifs) // return error message if file cannot be opened
{
cerr << "Error: could not find the specified file" << endl;
return 1;
}
for (int i = 0; i < 11000; i++)
{
if (ifs >> dataVector[i]) //read in array
cout << dataVector[i] << endl;
else //if element cannot be read, return error
{
cout << "Failed to read." << endl;
break;
}
}
ifs.close(); //closes the file
system("pause");
return 0;
}
我有什么遗漏导致这个问题吗?我的代码没有返回任何编译器错误,我的检查没有错误,我的文本文件IS在正确的位置。