阅读多个列表
Reading multiple lists
本文关键字:列表 更新时间:2023-10-16
我正在做一个项目,我需要从一个文本文件中读取多个列表。除了读取它们之外,它还报告一些基于所读入的整型数的计算。文件读取在列表开头给定的哨兵处结束。
我用来测试的列表是:9999 //sent value
636.9 // number to be ignored
First
4
8
15
16
23
42
Second
0.0
9999
我遇到的问题是,我的代码正确地读取并存储了第一个列表的数据,但是当循环再次执行时,它没有读取第二个列表。
我的主循环是:
infile >> sent;
infile >> inX;
while (!infile.fail())
{
inX = 0;
while (inX != sent)
{
int dSize = 0;
storeNames(listNames, infile);
ReadData(theData, infile);
cout << listNames[count] << endl;
cout << "Size isttt" << theData.size() << endl;
if (theData.size() >= 1)
{
ComputeStats(theData, meanValue, secondHigh);
DisplayStats(meanValue, secondHigh);
dSize = theData.size();
inX = theData[dSize - 1];
}
theData.clear();
count++;
}
}
读取数据函数为:
void ReadData(vector<int>& theData, ifstream& in)
{
if (in.is_open())
{
int number;
while (in >> number)
{
theData.push_back(number);
}
}
}
最后storeNames函数是:
void storeNames(vector<string>& listNames, ifstream& in)
{
string name;
in >> name;
listNames.push_back(name);
}
提前感谢您的帮助!
考虑到上面的代码,在流状态失败后,您是否恢复它?readData将消耗所有内容,之后需要恢复流的状态,如果想再次读取,则需要将seekg移回。while (in>> number)
可以在这里找到恢复istream状态
是否需要移动seekg取决于您是否想再次使用相同的数据,否则您可能需要将流指针移动到下一个数据节。
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题