对于家庭作业,我需要打开一个文件并从中读取数字。然后将每个数字分配给一个数组。但是,最后一个数字总是加到数组两次。
文件中的数字 8 1 9 1 7 2 10 8 2 6 2 7 3 10 10 10 8 1
输出看起来像这样 针[0] = 8针1 = 1 ...针[16] = 8针[17] = 1针[18] = 1
我的代码
void arrayFiller(int arrayP[])
{
ifstream cleanout9;
cleanout9.open("cleanlane9.txt");
while(cleanout9.fail())
{
cout<< "fail to open file";
exit(-1);
}
int i=0;
while(! cleanout9.eof())//&&i<18) If I add this, I can control the output, do I have other methods?
{
int num;
cleanout9>>num;
arrayP[i]=num;
cout<<"\npin["<<i<< "]"<<"="<<arrayP[i]<<" ";
i++;
}
cleanout9.close();
}