将文件中的值存储到数组中,并根据特定数字提取值
Storing values from file into array and extract values based on certain number
file.txt
0 00 19
0 200 699
0 7000 8499
0 85000 89999
0 900000 949999
0 9500000 9999999
1 00 09
1 100 399
1 4000 5499
1 55000 86979
1 869800 998999
1 9990000 9999999
2 00 19
2 200 349
2 35000 39999
2 495 699
2 7000 8399
2 84000 89999
2 900000 949999
2 9500000 9999999
3 00 02
3 030 033
3 0340 0369
3 03700 03999
3 04 19
3 200 699
3 7000 8499
3 85000 89999
3 900000 949999
3 9500000 9999999
假设我只想要第一个值3
的块,我怎么能只将以下内容存储到三个单独的数组中:
3 00 02
3 030 033
3 0340 0369
3 03700 03999
3 04 19
3 200 699
3 7000 8499
3 85000 89999
3 900000 949999
3 9500000 9999999
到目前为止,我有:
while (finished != EOF){
finished = fscanf(fp,"%d %s %sn", &areaScanned, &pubLow, &pubHigh);
}
这工作正常,但它存储整个列表。我怎样才能缩小范围?基本上我需要做的是检查某个数字是否落在第 1 行的值之间,第 1 列和第 2 列之间,然后是第 2 行,值介于 col1 和 col2 之间,然后是行 3,值介于 col1 和 col2 之间等。
首先,您可能不想使用 fscanf
。其次,你几乎肯定不想使用三个单独的数组,如果你能帮上忙的话。如果可能的话,你想要这样的东西:
struct whatever {
int a, b, c;
friend std::istream &operator>>(std::istream &is, whatever &w) {
return is >> w.a >> w.b >> w.c;
}
};
std::vector<whatever> w;
std:copy_if(std::istream_iterator<whatever>(infile),
std::istream_iterator<whatever>(),
std::back_inserter(w),
[](whatever const &w) { return w.a == 3; } );
相关文章:
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何从适合一定范围的数字中提取数字?
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 从迭代器中提取数字
- 如何安全地将有符号字段从uint32_t提取到有符号数字(int或uint32_t)中
- 如何从txt文件c++中提取括号之间的数字
- 如何将ASCII数字转换为从文件中提取的整数
- 如何提取给定数字的 p MSB 位
- printf如何从浮点数字中提取数字
- 使用boost::filesystem和boost::lexical_cast从文件名中提取数字
- 在循环中使用字符串流从几个字符串中提取数字
- 从字母数字 QString 中提取编号
- 从C++字符串中提取数字
- C 正则是从字符串中提取所有可能的10位数字
- 从数组中提取一对数字
- 尝试编写函数尝试从字符串中提取数字,但它返回不相关的数字
- 接受多个输入(如 +、- 和平方数字)的计算器.从文本文件中提取信息
- 长方程加法/减法计算器,从文本文件中提取运算符和数字
- 仅使用关键字和数字提取来解析简单语法
- 将文件中的值存储到数组中,并根据特定数字提取值