读取多达 'n' 的数据

read data up to ' '

本文关键字:数据 读取      更新时间:2023-10-16

我的程序有一个输入文件,其中存储了用于两个参数的值。

# X parameter values to use
10 20 30 40
# Y parameter values to use
5

值的数量不是固定的,而是客户端选择使用多少值(在本例中,X为4,Y为1(也可以是0))。如果我输入

ifstream in("input.in");
int value;
in >> value >> value >> ...

程序不会在行尾停止,并且在X行结束时显然也会读取Y行中的X值。如何在一行结束时停止阅读?我想读取值,直到找到n。谢谢!

std::string line;
std::getline(in, line);
现在您有了字符串形式的行( #include <string> ),需要对其进行解析。你可以像以前一样使用std::istringstream ( #include <sstream> )。
std::istringstream iss(line);
while (iss >> value) // read until failure
{
    // process value
}