Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文件中的值存储到数组中,并根据特定数字提取值_C++ - Fatal编程技术网

C++ 将文件中的值存储到数组中,并根据特定数字提取值

C++ 将文件中的值存储到数组中,并根据特定数字提取值,c++,C++,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

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 %s\n", &areaScanned, &pubLow, &pubHigh);
}

这很好,但它存储了整个列表。我怎样才能缩小范围?基本上,我需要做的是检查某个数字是否落在第1行的值之间、第1列和第2列之间、第2行、第1列和第2列之间的值、第3行、第1列和第2列之间的值等等。

首先,您可能不想使用
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; } );
struct which{
INTA、b、c;
friend std::istream&operator>>(std::istream&is,whatever&w){
返回值为>>w.a>>w.b>>w.c;
}
};
std::向量w;
std:copy_if(std::istream_迭代器(infle),
std::istream_迭代器(),
标准:背面插入器(w),
[](无论什么常量&w){return w.a==3;});

首先,您可能不想使用
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; } );
struct which{
INTA、b、c;
friend std::istream&operator>>(std::istream&is,whatever&w){
返回值为>>w.a>>w.b>>w.c;
}
};
std::向量w;
std:copy_if(std::istream_迭代器(infle),
std::istream_迭代器(),
标准:背面插入器(w),
[](无论什么常量&w){return w.a==3;});

三个独立的阵列是什么意思?每一列应该包含什么?我理解为将每一列存储到一个数字数组中。但是现在,当他发布那篇
fscanf
调用时,我很困惑为什么他把其他专栏读成
%s
@LihO,哦,这更有意义。你说的三个单独的数组是什么意思?每一列应该包含什么?我理解为将每一列存储到一个数字数组中。但是现在,当他发布了
fscanf
调用时,我很困惑,为什么他将其他列读为
%s
@LihO,哦,这更有意义。+1我认为他希望最后的容器不灵活,每行的第一个值在他的数据文件中是连续的,但他对此相当不清楚。老实说,鉴于问题及其呈现的模糊性,这是一个非常可靠的答案。+1用于让对象读取其字段值。此代码太复杂,远远超出了我们在课堂上学习的范围。我一点也不懂。+1我想他希望最后的容器不灵活,每行的第一个值,在他的数据文件中是连续的,但他对此相当不清楚。老实说,鉴于问题及其呈现的模糊性,这是一个非常可靠的答案。+1用于让对象读取其字段值。此代码太复杂,远远超出了我们在课堂上学习的范围。我一点也不懂。