繁体   English   中英

读取二进制文件并计算特定数量的C ++

[英]Read binary file and count specific number c++

嘿,我一直在到处寻找有关如何执行此特定任务的见解。 我看到了类似的内容,但没有明确的解释。 我正在尝试读取bin文件并计算特定数字出现的次数。 我看到了使用.txt文件的示例,并且使用getline看起来非常简单。 我试图复制类似的结构,但使用二进制文件。

int main() {

    int searching = 3;
    int counter = 0;
    unsigned char * memblock;
    long long int size;

    //open bin file
    ifstream file;
    file.open("threesData.bin", ios:: in | ios::binary | ios::ate);

    //read bin file
    if (file.is_open()) {
        cout << "it opened\n";
        size = file.tellg();
        memblock = new unsigned char[size];
        file.seekg(0, ios::beg);
        file.read((char * ) memblock, size);

        while (file.read((char * ) memblock, size)) {
            for (int i = 0; i < size; i++) {
                (int) memblock[i];
                if (memblock[i] == searching) {
                    counter++;
                }
            }

        }
    }

    file.close();
    cout << "The number " << searching << " appears ";
    cout << counter << " times!";
    return 0;
}

当我运行该程序时,很明显它会打开,但不计算我要搜索的数字。 我究竟做错了什么?

您似乎一直在考虑这个问题,但是这就是我要做的事情。

  1. 初始化大小合适的缓冲区。
  2. 将其强制转换为整数,因此可以使用array[size_t]语法进行更简单的运算。
  3. 打开流,并在流有效时阅读。
  4. 将读取的字节数转换为所需的整数数。
  5. 为您发现有效的每个字符增加计数器。

#include <fstream>
#include <iostream>


bool check_character(int value)
{
    return value == 3;
}


int main(void)
{
    // choose the size, cast a pointer as an int type, and initialize
    // our counter
    static constexpr size_t size = 4096;
    char* buffer = new char[size];
    int* ints = (int*) buffer;
    size_t counter = 0;

    // create our stream, 
    std::ifstream stream("file.bin", std::ios_base::binary);
    while (stream) {
        // keep reading while the stream is valid
        stream.read(buffer, size);
        auto count = stream.gcount();

        // we only want to go to the last valid integer
        // if we expect the file to be only integers,
        // we could do `assert(count % sizeof(int) == 0);
        // otherwise, we may have trailing characters

        // if we have trailing characters, we may want to move them
        // to the front of the buffer....
        auto chars = count / sizeof(int);           // floor division
        for (size_t i = 0; i < chars; ++i) {
            // false == 0, true == 1, so we can just add
            // if the value is 3
            counter += check_character(ints[i]);
        }
    }

    std::cout << "Counter is: " << counter << std::endl;

    delete[] buffer;
    return 0;
}

正如NeilButterworth指出的那样,您也可以使用向量。 我不是很喜欢这个,但是“嗯”。

#include <fstream>
#include <iostream>
#include <vector>

/* ellipsed lines */

int main(void)
{
    /* ellipsed lines */
    static constexpr size_t size = 4096;
    std::vector<int> ints;
    ints.resize(size / sizeof(int));
    char* buffer = (char*) ints.data();
    /* ellipsed lines */

    /* ellipsed lines */
    std::cout << "Counter is: " << counter << std::endl;
    // no delete[]
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM