【发布时间】:2014-10-11 14:53:30
【问题描述】:
我必须编写一个程序来读取这样的文件:
7
5 6 4 2 1 3 8
第一行表示有多少人,第二行表示每个人的身高。我设法读取了第一行并将其存储在一个变量中,但是如何继续第二行单独读取每个整数(它们用空格分隔)
using namespace std;
int rowNum;
int main()
{
fstream myfile;
string rowNumT;
myfile.open ("xxx_in.txt",ios::in | ios::out);
if(myfile.is_open()){
while(getline(myfile,rowNumT)){
//cout << rowNumT ;
istringstream (rowNumT) >> rowNum;
cout << rowNum ;//how many children in integer form
}
}
else cout << "Unable to open file";
int heights[rowNum];
myfile.close();
return 0;
}
【问题讨论】:
-
你甚至不需要
getline。直接从文件中一次读取一个数字。
标签: c++ arrays file input output