Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6075907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:32:48+00:00 2026-05-23T10:32:48+00:00

I have asked this question a few days ago: How to look for an

  • 0

I have asked this question a few days ago:

How to look for an ANSI string in a binary file?

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.

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

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?

Possibly, can you show me

  1. a version where the file is being copied to memory (supposing the binary file is small)

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:32:49+00:00Added an answer on May 23, 2026 at 10:32 am

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guys I've asked few days ago a question and didn't have really time to
I asked a pretty similar question a few days ago, but this one is
Hello Everybody i asked this question few hours ago C# get username from string.
I asked this question a few days ago, and I didn't get a lot
Few days ago I have asked a question about 1,2 and 3. degree connections.
I originally asked this question on the Highcharts forum a few days ago but
I asked a question almost identical to this a few days ago via this
I have just asked this question an hour ago but with regards to IE8
I had asked this question in a much more long-winded way a few days
A few days ago I has asked a question on SO about helping me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.