code在二进制文件中搜索字符串 [英] Code for searching for a string in a binary file

查看:155
本文介绍了code在二进制文件中搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也问过这个问题,前几天:

I have asked this question a few days ago:

<一个href=\"http://stackoverflow.com/questions/6447819/how-to-look-for-an-ansi-string-in-a-binary-file\">How找一个二进制文件为ANSI字符串?

和我有一个非常好的<一个href=\"http://stackoverflow.com/questions/6447819/how-to-look-for-an-ansi-string-in-a-binary-file/6447881#6447881\">answer,什么后来变成一个更难的问题:<一href=\"http://stackoverflow.com/questions/6449266/can-input-iterators-be-used-where-forward-iterators-are-expected\">Can使用输入迭代器,其中前向迭代器有望?的什么现在是真的不上什么样的水平,我可以理解。

and I got a really nice answer, what later turned into a much harder question: Can input iterators be used where forward iterators are expected? what is now really not on a level what I could understand.

我仍然在学习C ++,我期待一种简单的方法来搜索二进制文件的字符串。

I am still learning C++ and I am looking for an easy way to search for a string in a binary file.

有人能告诉我一个简单的code的简约C ++控制台程序这会在一个二进制文件中的字符串,并输出位置到stdout?

Could someone show me a simple code for a minimalistic C++ console program which looks for a string in a binary file and outputs the locations to stdout?

可能的话,你能告诉我


  1. 其中文件正被复制到内存中的版本(假定二进制文件小)

和它使用的有道从链接的问题的另一种

and an other one which uses the proper way from the linked questions

如果这听起来像我要求别人的code,但我刚学C ++,我想,也许其他人可以从这个问题中获益,如果有人可以张贴一些高品质的code什么是好的,对不起借鉴。

Sorry if it sounds like I'm asking for someone's code, but I am just learning C++ and I think maybe others could benefit from this question if someone could post some high quality code what is nice to learn from.

推荐答案

您要求规范不清,例如 - 在哪里呢121出现在12121...只是在第一个字符(之后搜索在继续4),或在第三个呢?下面的code使用前一种方法。

Your requirement specification is unclear, for example - where does "121" appear in "12121"... just at the first character (after which searching continues at the 4th), or at the 3rd as well? The code below uses the former approach.

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>

int main(int argc, const char* argv[])
{
    if (argc != 3)
    {
        std::cerr << "Usage: " << argv[0] << " filename search_term\n"
            "Prints offsets where search_term is found in file.\n";
        return 1;
    }

    const char* filename = argv[1];
    const char* search_term = argv[2];
    size_t search_term_size = strlen(search_term);

    std::ifstream file(filename, std::ios::binary);
    if (file)
    {
        file.seekg(0, std::ios::end);
        size_t file_size = file.tellg();
        file.seekg(0, std::ios::beg);
        std::string file_content;
        file_content.reserve(file_size);
        char buffer[16384];
        std::streamsize chars_read;

        while (file.read(buffer, sizeof buffer), chars_read = file.gcount())
            file_content.append(buffer, chars_read);

        if (file.eof())
        {
            for (std::string::size_type offset = 0, found_at;
                 file_size > offset &&
                 (found_at = file_content.find(search_term, offset)) !=
                                                            std::string::npos;
                 offset = found_at + search_term_size)
                std::cout << found_at << std::endl;
        }
    }
}

这篇关于code在二进制文件中搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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