使用STL搜索原始字节并在文件中替换它,什么是最好的/正确的方法 [英] Using STL to search for raw bytes and replace it in a file, what is the best / correct way

查看:196
本文介绍了使用STL搜索原始字节并在文件中替换它,什么是最好的/正确的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用STL(vs2010中的C ++ 0x)来打开文件和二进制搜索,然后在找到匹配项时替换。

I'd like to use the STL (C++0x in vs2010) to open a file and binary search through it and then replace when a match is found.

我可以使用fstream打开一个文件,但我有点困惑,如何搜索文件。

I can use fstream to open a file but i'm a bit confused to how to search the file.

一次读取一个字节不好,它应该将一个块读入缓冲区,然后在缓冲区中搜索,但是这个功能已经在STL中了?

Reading one byte at a time isn't good so it should read a block into a buffer and we then search in the buffer but is this functionality already in the STL ?

我想要缓冲部分是自动的,当搜索到达缓冲区的结尾,它应该从文件中自动读入下一个块,如同它直接读取(使用相同的文件偏移量等)。

I want the buffering part to be automatic, when the search reach the end of buffer it should automatically read in the next block from the file as if it was reading it directly (using the same file offsets, etc..)

我知道这个文件的功能存在于windows中使用CreateFileMapping和 MapViewOfFile 但我想使用STL让我的代码可移植并且通过使用STL也更加灵活。使用这些窗口功能,您可以读取文件而不必担心缓冲等,他们甚至会更新文件,当您更改数据。这是我使用STL后的功能。

I know that this file functionality exists in windows using CreateFileMapping and MapViewOfFile but i want to use the STL to make my code portable and also by using the STL also more flexible. Using these windows function you can read the file without worrying about buffering, etc, they will even update the file when you change data. This is the functionality i'm after using the STL.

更新:我改变了'二进制搜索'

Update: I've changed 'binary search' to 'raw byte search' to clear up the confusion, sorry for that.

原始字节搜索功能的一个例子(如果你有更好,请告诉)。

An example of the raw byte search function ( if you have a better please do tell )

// NOTE: This function doesn't support big files > 4 gb
DWORD_PTR RawByteSearch( PBYTE pBaseAddress, DWORD dwLowSize, PBYTE pSignature, DWORD sigSize )
{
 PBYTE pBasePtr = pBaseAddress;
 PBYTE pEndPtr = pBaseAddress + dwLowSize;
 DWORD_PTR i;
 while ( pBasePtr < pEndPtr )
 {
  for (i = 0; i < sigSize; ++i)
  {
   if ( pSignature[i] != pBasePtr[i] )
    break;
  }
  if ( i == sigSize ) // Found a match
  {
   return pBasePtr - pBaseAddress;
  }
  ++pBasePtr; // Next byte
 }
 return 0;
}


推荐答案

std :: search 已经做了你需要的。

std::search already does what you need.

std::search(pBasePtr, bEndPtr, pSignature, pSignature+sigSize)

出于完整性的考虑,您不需要使用内存映射(尽管这可能是最高效的)。

Out of completeness, you don't need to use memory mapping (although that will likely be the most efficient).

100%的可移植解决方案是使用标准库流迭代器:

A 100% portable solution is to instead use the standard library stream iterators:

std::ifstream file;
std::search(std::istreambuf_iterator<char>(file.rdbuf()) // search from here
          , std::istreambuf_iterator<char>()             // ... to here
          , pSignature                                   // looking for a sequence starting with this
          , pSignature+sigSize);                         // and ending with this

这篇关于使用STL搜索原始字节并在文件中替换它,什么是最好的/正确的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆