如何在二进制文件中查找ANSI字符串? [英] How to look for an ANSI string in a binary file?
问题描述
我想使用C ++在二进制文件中找到ANSI字符串的第一次出现。
I would like to find the first occurence of an ANSI string in a binary file, using C++.
我知道字符串类有一个方便的find函数,我不知道如何使用它,如果文件是大的,说5-10 MB。
I know the string class has a handy find function, but I don't know how can I use it if the file is big, say 5-10 MB.
我需要先将整个文件复制到内存中的字符串中吗?如果是,我如何确保二进制在复制时,字符被破坏?
Do I need to copy the whole file into a string in memory first? If yes, how can I be sure that none of the binary characters get corrupted while copying?
或者是否有更有效的方法来完成它,而不需要将其复制到字符串中? / p>
Or is there a more efficient way to do it, without the need for copying it into a string?
推荐答案
我需要将整个文件复制到内存中的字符串中吗?
Do I need to copy the whole file into a string in memory first?
否。
Or is there a more efficient way to do it, without the need for copying it into a string?
当然,使用 std :: ifstream
(请务必在二进制模式中打开,而不是文本模式),请创建一对 multi_pass
迭代器(来自 Boost )。 精神),然后搜索字符串与 std :: search
。
Of course; open the file with an std::ifstream
(be sure to open in binary mode rather than text mode), create a pair of multi_pass
iterators (from Boost.Spirit) around the stream, then search for the string with std::search
.
这篇关于如何在二进制文件中查找ANSI字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!