【发布时间】:2020-11-22 04:31:46
【问题描述】:
在 C++ 中,我应该读取一个文件,其中每一行都包含一个整数,并将每个整数传输到一个数组中。我尝试使用 getline() 函数计算行数。并创建了数组,但是当我计算行数时,它会消耗,如果我再次使用 getline() 函数,它将不起作用。我该怎么办?谢谢。
ifstream inFile( fileName );
if ( inFile.is_open() ) {
int size = 0;
string line;
while( getline(inFile, line))
size++;
int* array = new int [ size ];
while ( getline( inFile, line )) {
....
}
}
The code does not enter the second while.
【问题讨论】:
-
您应该将读取的整数添加到
std::vector。 -
请分享您描述的代码。
-
编辑问题,不要使用 cmets。另外,请确保发布minimal reproducible example