istringstream未输出正确的数据
istringstream not outputting correct data
在下面显示的while循环中,我很难让istringstream继续。数据文件也显示在下面。我使用输入文件中的getline来获取第一行,并将其放在istringstream-lineStream中。它通过while循环一次,然后读取第二行,返回到循环的开头并退出,而不是继续通过循环。我不知道为什么,如果有人能帮忙,我会很感激。编辑:我之所以有这种while循环条件,是因为文件可能包含错误的数据行。因此,我想确保我正在读取的行在数据文件中具有如下所示的正确形式。
while(lineStream >> id >> safety){//keeps scanning in xsections until there is no more xsection IDs
while(lineStream >> concname){//scan in name of xsection
xname = xname + " " +concname;
}
getline(InputFile, inputline);//go to next xsection line
if(InputFile.good()){
//make inputline into istringstream
istringstream lineStream(inputline);
if(lineStream.fail()){
return false;
}
}
}
数据文件
4 0.2 speedway and mountain
7 0.4 mountain and lee
6 0.5 mountain and santa
在给出的代码中,…
while(lineStream >> id >> safety){//keeps scanning in xsections until there is no more xsection IDs
while(lineStream >> concname){//scan in name of xsection
xname = xname + " " +concname;
}
getline(InputFile, inputline);//go to next xsection line
if(InputFile.good()){
//make inputline into istringstream
istringstream lineStream(inputline);
if(lineStream.fail()){
return false;
}
}
}
…lineStream
的内部声明声明了一个本地对象,当执行超出该块时,该对象将不存在,并且不会影响外部循环中使用的流。
一个可能的解决方案是稍微反转代码,如下所示:
while( getline(InputFile, inputline) )
{
istringstream lineStream(inputline);
if(lineStream >> id >> safety)
{
while(lineStream >> concname)
{
xname = xname + " " +concname;
}
// Do something with the collected info for this line
}
}
相关文章:
- 输入数据成员未按要求工作
- 从 XML 中读取未指定结构的每个数据成员
- 数据文件的第二行未正确读取
- 我的头文件 DNode 中的我的数据类型未被识别为数据类型
- 数据未插入 MySQL databae
- 从 std::string 到 std::array<char,size> 的 memcopy 额外数据是否是一种未定义的行为?
- 用数据填充未指定大小的数组
- 在 C++17 中,是否未定义使用无锁原子学保护从信号处理程序传递的数据?
- 深度缓冲区未填充阴影贴图渲染通道中的数据
- 类的成员数据未更新
- 访问位域联合是C++标准中常见的初始数据未定义行为
- 重复数据未从矢量中删除
- 输入数据未分配给SQL查询
- QTableView 不响应数据未聚焦时更改
- 通过 JNI 的数据未正确传递
- 数据未保存在文本文件中(C++ Fstream 库)
- 避免在数据未更改时重新计算
- 在Qt套接字中,写入数据未到达服务器
- 数据未存储在阵列中
- Arduino串行端口,数据未到达