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 8039359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:32:41+00:00 2026-06-05T03:32:41+00:00

I think that’s not a difficult question but i want to know how to

  • 0

I think that’s not a difficult question but i want to know how to do it in a C++ way. I want to search the position of a defined word or sequence.

I read this post stack similar question and the answer looks great. But i don’t know how to apply this solution with a file. This std instruction it’s easy to apply in a or a but i don’t know how to apply to a file and a sequence.

Instruction:

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

Can I use a string to store the sequence to search in the file ???

Could someone post an easy example of how to apply the search instruction, i always obatin and big error when I compile it.

I don’t use windows and don’t want to use Boost library.

Thanks in advance.

  • 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-06-05T03:32:42+00:00Added an answer on June 5, 2026 at 3:32 am

    Read the file into a string (assuming it’s not huge). You can then use string::find or std::algorithm.

    using namespace std;
    
    // read entire file into a string
    ifstream file(".bashrc");
    string contents((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
    
    string needle = "export";
    
    // search using string::find
    size_t pos = contents.find(needle);
    cout << "location using find: " << contents.find(needle) << endl;
    
    // search using <algoritm>'s search
    string::const_iterator it = search(contents.begin(), contents.end(), needle.begin(), needle.end());
    cout << "location found using search: " << string(it, it + 10) << endl;
    cout << "    at position: " << it - contents.begin() << endl;
    

    [EDIT]
    You can also search with the istreambug_iterators directly, but that leaves you with the same kind of iterator.

    istreambuf_iterator<char> it2 = search(
            istreambuf_iterator<char>(file), istreambuf_iterator<char>(),
            needle.begin(), needle.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think that it's really crazy question but anyway I want to know if
I think that this is a relatively simple question. I just want to know
I think that's a rather simple Question but I don't get it the way
I think that I am having a slow evening but I have a question
I think that this should be easy, but I'm not able to get it
I think that's a very simple question, but I can't deal with it: I
I think that's an easy question but couldn't find the solution. I redirect the
I think that this is a permissions issue but I am not sure and
I think that the title accurately summarizes my question, but just to elaborate a
I think that this is a newbie type question but I have quite understood

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.