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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:45:42+00:00 2026-06-05T12:45:42+00:00

I have a program in C++ that needs to return a line that a

  • 0

I have a program in C++ that needs to return a line that a specific word appears in. For instance, if my file looks like this:

the cow jumped over
the moon with the
green cheese in his mouth

and I need to print the line that has “with”. All the program gets is the offset from the beginning of the file (in this case 24, since “with” is 24 characters from the beginning of the file).

How do I print the whole line “the moon with the”, with just the offset?

Thanks a lot!

  • 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-05T12:45:44+00:00Added an answer on June 5, 2026 at 12:45 pm

    A good solution is reading the file from the beginning until the desired position (answer by @Chet Simpson). If you want optimization (e.g. very large file, position somewhere in the middle, typical lines rather short), you can read the file backwards. However, this only works with files opened in binary mode (any file on unix-like platforms; open the file with ios_base::binary parameter on Windows).

    The algorithm goes as follows:

    • Go back a few bytes in file
    • Read the few bytes
    • If there is an end-of-line there, the rest is easy
    • Otherwise, repeat

    Code (tested on Windows):

    std::string GetSurroundingLine(std::istream& f, std::istream::pos_type start_pos)
    {
        std::istream::pos_type prev_pos = start_pos;
        std::istream::pos_type pos;
        char buffer[40]; // typical line length, so typical iteration count is 1
        std::istream::pos_type size = sizeof(buffer);
    
        // Look for the beginning of the line that includes the given position
        while (true)
        {
            // Move back 40 bytes from prev_pos
            if (prev_pos < size)
                pos = 0;
            else
                pos = prev_pos - size;
            f.seekg(pos);
    
            // Read 40 bytes
            f.read(buffer, prev_pos - pos);
            if (!f)
                throw;
    
            // Look for a newline byte, which terminates previous line
            int eol_pos;
            for (eol_pos = sizeof(buffer) - 1; eol_pos >= 0; --eol_pos)
                if (buffer[eol_pos] == '\n')
                    break;
    
            // If found newline or got to beginning of file - done looking
            if (eol_pos >= 0 || pos == (std::istream::pos_type)0)
            {
                pos += eol_pos + 1;
                break;
            }
        }
    
        // Position the read pointer
        f.seekg(pos);
    
        // Read the line
        std::string s;
        std::getline(f, s, '\n');
    
        return s;
    }
    

    Edit: On Windows-like platforms, where end-of-line is marked by \r\n, since you have to use binary mode, the output string will contain the extra character \r (unless there is no end-of-line at end-of-file), which you can throw away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that looks like this. I need to consistently write something
I have a program that reads a huge text file (line by line) and
I have functions in my program that stop after it reaches a Return line
I have a program that returns something like this: status: playing artURL: http://beta.grooveshark.com/static/amazonart/m3510922.jpg estimateDuration:
I have a program that needs several third-party libraries, and at the moment it
I have a program that needs a lot of memory and want to set
I have a Flash program that needs to make url requests to send data
In an embedded program I have a screen object that needs to manage a
I have a program that I need to be able to search a file
I have the need to use a Stack-like data structure for a program that

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.