在大型二进制文件 C++ 中查找 int 值

Finding int value in large binary file c++

我试图制作一个程序来加载大文件块(我们说的是几 MB),搜索一个值,并打印它的地址和值,除了我的程序每隔几次抛出一个 !myfile ,除了一个奇怪的符号之外没有给出值(虽然我在 cout 中使用了 'hex' ),地址似乎循环排序,并且它似乎根本没有找到所有值。我已经尝试了很长时间但我放弃了,所以我向那里的经验丰富的编码人员询问问题所在。 我应该注意,我试图在这个文件中找到一个 32 位值,但我所能做的只是一个检查字节的程序,我也需要帮助。

#include <iostream>
#include <fstream>
#include <climits>
#include <sstream>
#include <windows.h>
#include <math.h>

using namespace std;

int get_file_size(std::string filename) // path to file
{
    FILE *p_file = NULL;
    p_file = fopen(filename.c_str(),"rb");
    fseek(p_file,0,SEEK_END);
    int size = ftell(p_file);
    fclose(p_file);
    return size;
}

int main( void )
{
    ifstream myfile;

    myfile.open( "file.bin", ios::binary | ios::in );


    char addr_start = 0,
                      addr_end   = 0,
                      temp2      = 0x40000;

    bool found = false;

   cout << "\nEnter address start (Little endian, hex): ";
   cin >> hex >> addr_start;
   cout << "\nEnter address end (Little endian, hex): ";
   cin >> hex >> addr_end;

    unsigned long int fsize = get_file_size("file.bin");
    char buffer[100];

    for(int counter = fsize; counter != 0; counter--)
    {
        myfile.read(buffer,100);
        if(!myfile)
        {
            cout << "\nAn error has occurred. Bytes read: " << myfile.gcount();
            myfile.clear();
        }
        for(int x = 0; x < 100 - 1 ; x++)
        {
            if(buffer[x] >= addr_start && buffer[x] <= addr_end)
                            cout << "Addr: " << (fsize - counter * x) << "  Value: " << hex << buffer[x] << endl;
        }

    }

    myfile.close();
    system("PAUSE"); //Don't worry about its inefficiency
}

一个在二进制文件中搜索 32 位整数的简单程序:

int main(void)
{
  ifstream data_file("my_file.bin", ios::binary);
  if (!data_file)
  {
    cerr << "Error opening my_file.bin.\n";
    EXIT_FAILURE;
  }
  const uint32_t search_key = 0x12345678U;
  uint32_t value;
  while (data_file.read((char *) &value, sizeof(value))
  {
    if (value == search_key)
    {
      cout << "Found value.\n";
      break;
    }
  }
  return EXIT_SUCCESS;
}

您可以通过读入缓冲区并搜索缓冲区来提高性能。

//...
const unsigned int BUFFER_SIZE = 1024;
static uint32_t  buffer[BUFFER_SIZE];
while (data_file.read((char *)&(buffer[0]), sizeof(buffer) / sizeof(uint32_t))
{
  int bytes_read = data_file.gcount();
  if (bytes_read > 0)
  {
    values_read = ((unsigned int) bytes_read) / sizeof(uint32_t);
    for (unsigned int index = 0U; index < values_read; ++index)
    {
       if (buffer[index] == search_key)
       {
         cout << "Value found.\n";
         break;
       }
    }
  }  
}

通过上面的代码,当读取失败时,应该检查字节数,如果读取了任何字节,则搜索缓冲区。