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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:00:12+00:00 2026-05-27T16:00:12+00:00

Possible Duplicate: In C++ is there a way to go to a specific line

  • 0

Possible Duplicate:
In C++ is there a way to go to a specific line in a text file?

Whats the smarter way to read specific set of lines (line number A to line number B) from a text file in C++ using standard C++ library (without opting to Boost)?

  • 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-27T16:00:13+00:00Added an answer on May 27, 2026 at 4:00 pm

    If the line length isn’t fixed and you don’t have an index you can’t do better than counting the \ns.

    Consider this example file:

    Hello\nThis is a multi\n-line\nfile, where is this?\n\nAgain?
    

    Line 1 starts at byte 0, Line 2 at 6, Line 3 at 22, Line 4 at 28, Line 5 at 49 and Line 6 at 50 – there’s no pattern.

    If we knew that information in advance, e.g. at the beginning of the file we had that information in some table we could compute a byte offset into the file for the lines we cared about and use a seek to jump straight there.

    If the line width is fixed at 20 bytes:

    Hello               \nThis is a multi     \n-line               \nfile, where is this?\n                    \nAgain?
    

    Then we can compute the start of a line as a simple multiplication – an offset into the file.


    If you’re looking for a “generic” way of doing this I’d suggest something like:

    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    template <typename Iter, typename Count, typename T>
    Iter find_nth(Iter it, const Iter end, Count c, const T match) {
      while(c > 0 && it != end) {
        if (match == *it++)
          --c;
      }
      return it;
    }
    
    int main() {
      std::ifstream in("test.txt");
      std::ostringstream str;
      str << in.rdbuf();
    
      const std::string& s = str.str();
      const int A=2, B=4;
      const std::string::const_iterator begin=find_nth(s.begin(),s.end(), A, '\n');
      const std::string::const_iterator end  =find_nth(begin,s.end(), B-A, '\n');
    
      const std::string range(begin,end);
      std::cout << range << std::endl;
    }
    

    This is appropriate for small-ish files (it reads the entire file into a std::string). For larger files you might want do do the same, but with mmap instead, using the mapped region as iterators. Or you could do this with a RandomAccess iterator that uses seek() within the file. (std::istream_iterator does not do this, it’s only a ForwardIterator so wouldn’t be appropriate).

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

Sidebar

Related Questions

Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
Possible Duplicate: Is there any way to render text into DirectWrite???? Hello, all is
Possible Duplicate: Calculate number of days between two given dates Is there a way
Is there any fast and memory efficient way to read specific lines of large
Possible Duplicate: Is there a way to set different CFBundleDisplayName for iPad and iPhone?
Possible Duplicate: Select div with specific width Is there a way to select all
Possible Duplicate: Is there a way to hide the scroll indicators in an UIScrollView?
Possible Duplicate: Is there a way to automatically update application on Android? As a
Possible Duplicate: Is there a way to automatically update application on Android? I have

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.