在大型二进制文件c ++

时间:2015-12-27 21:58:59

标签: c++ file compare buffer fstream

我尝试制作一个程序来加载一大块文件(我们说几个MB)的文件,并搜索一个值,然后打印它的地址和值,除了我的程序每隔几次抛出一次a!myfile,除了一个奇怪的符号之外没有给出值(虽然我已经使用了' hex'在cout中),地址似乎循环排序,但它并没有似乎找到了所有的价值观。我已经尝试了很长时间而且我放弃了,所以我要求经验编码员找到问题。 我应该注意到我试图在这个文件中找到一个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
}

1 个答案:

答案 0 :(得分:3)

在二进制文件中搜索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;
       }
    }
  }  
}

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