首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++怎么一行一行地读整数

2012-03-14 
c++如何一行一行地读整数?比如这样一组不知道多少行的数:735 34 1256 53 350633 4500 306 1001 50 1735 00

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
...
我想一行一行地读入到整型数组,然后处理
如何做?不想读一行字符串再自己分离

[解决办法]

C/C++ code
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;
}

热点排行