Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing STL to search for raw bytes and replace it in a file, what is the best / correct way
    text
    copied!<p>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.</p> <p>I can use fstream to open a file but i'm a bit confused to how to search the file.</p> <p>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 ?</p> <p>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..)</p> <p>The other problem is when it finds a match exactly how should it update the file.</p> <p>I know that this file functionality exists in windows using CreateFileMapping and <a href="http://msdn.microsoft.com/en-us/library/aa366761%28v=vs.85%29.aspx" rel="nofollow">MapViewOfFile</a> 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.</p> <p><strong>Update</strong>: I've changed 'binary search' to 'raw byte search' to clear up the confusion, sorry for that.</p> <p>An example of the raw byte search function ( <strong>if you have a better please do tell</strong> )</p> <pre><code>// NOTE: This function doesn't support big files &gt; 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 &lt; pEndPtr ) { for (i = 0; i &lt; sigSize; ++i) { if ( pSignature[i] != pBasePtr[i] ) break; } if ( i == sigSize ) // Found a match { return pBasePtr - pBaseAddress; } ++pBasePtr; // Next byte } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload