有txt按行记录部分数据如:
123
456
789
...
要将数据读取到数组int a[100]中
a[0]=123,
a[1]=456,
a[2]=789依此类推;
求解答。。。C++
///代码在Dev-C++ 4.9.9.2环境下编译成功!
///new.txt为当前目录下的文件,格式如你所说.
#include <iostream>
#include <fstream>
int main()
{
std::ifstream in("number.txt");
if(not in)
return EXIT_FAILURE;
int a[100] = {0};
int index(0);
int x(0);
while(in >> x)
{
if(index > 99)
{
std::cout << "The array is full of number!\n";
break;
}
a[index] = x;
++index;
}
std::cout << "There are " << index << " numbers in the array!\n";
///print the array
if(not (index == 0))
for(int i=0;i<index;++i)
std::cout << "a[" << i << "] = " << a[i] << "\n";
system("pause");
return 0;
}