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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:30:43+00:00 2026-05-28T16:30:43+00:00

Can I open an ifstream (or set an existing one in any way) to

  • 0

Can I open an ifstream (or set an existing one in any way) to only read part of a file? For example, I would like to have my ifstream read a file from byte 10 to 50. Seeking to position 0 would be position 10 in reality, reading past position 40 (50 in reality) would resualt in an EOF, etc. Is this possible in any way?

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

    It definetly can be done by implementing a filtering stream buffer: you would derive from std::streambuf and take the range you want to expose and the underlying stream buffer (well, a pointer to it) as arguments. Then you would seek to the start location. An overridden underflow() function would read from the underlying stream buffer into its buffer until has consumed as many characters as were desired. Here is a somewhat rough and entirely untested version:

    #include <streambuf>
    struct rangebuf: std::streambuf {
        rangebuf(std::streampos start,
                        size_t size,
                        std::streambuf* sbuf):
            size_(size), sbuf_(sbuf)
        {
            sbuf->seekpos(start, std::ios_base::in);
        }
        int underflow() {
            size_t r(this->sbuf_->sgetn(this->buf_,
                std::min<size_t>(sizeof(this->buf_), this->size_));
            this->size -= r;
            this->setg(this->buf_, this->buf_, this->buf_ + r);
            return this->gptr() == this->egptr()
                ? traits_type::eof()
                : traits_type::to_int_type(*this->gptr());
        }
        size_t size_;
        std::streambuf* sbuf_;
    };
    

    You can use a pointer to an instance of this stream buffer to initialuze an std::istream. If this a recurring need, you might want to create a class derived from std::istream setting up the stream buffer instead.

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

Sidebar

Related Questions

If I do something like the following: ifstream file; file.open(somefile, ios::binary); unsigned int data;
I can open the DefaultTemplate.xaml that was installed as part of our TFS 2010
How i can open one Popover Controller from another Popover Controller in iPad?I want
If I can open a connection to an MS Access file in C#, how
I can't seem to open a file without storing it in a variable. I
What I'm attempting to accomplish, is to open an external .dat file using ifstream
I need to open a file as std::fstream (or actually any other std::ostream) when
Hi I want to read a txt file of hashed values using ifstream and
In C++, is there a case where std::ifstream open() can be successful, but std::ifstream
I can't figure out why this won't read from my file... #include <iostream> #include

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.