c++如何一行一行地读整数?
比如这样一组不知道多少行的数:
735 3 4 125 6 5 3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10
...
我想一行一行地读入到整型数组,然后处理
如何做?不想读一行字符串再自己分离
[解决办法]
string line;stringstream ss;while (getline(ifs, line)){ ss << line; int i = 0; while (ss >> arr[i++]);}
[解决办法]
[code=C/C++][/code]#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
stringstream stream;
int result[10];
string line;
ifstream fin;
fin.open("kk.txt");
while(!fin.eof())
{
getline(fin,line);
stream.clear(); //在进行多次转换前,必须清除stream
stream << line;
for(int i=0;i<10;i++)
stream >> result[i];
for(i=0;i<10;i++)
cout << result[i] << std::endl;
}
return 1;
}