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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:46:22+00:00 2026-05-27T00:46:22+00:00

I am trying to achieve something like this: while (ifstream has not been entirely

  • 0

I am trying to achieve something like this:

while (ifstream has not been entirely read)
{
   read a chunk of data into a buffer that has size BUFLEN
   write this buffer to ostream
}

At first I tried to achieve this by using ifstream.eof() as my while condition, but I’ve heard that this is not the way to go. I’ve been looking at std::ios::ifstream’s other functions, but can’t figure out what else to use.

PS: I am using a buffer because the file that is being transferred can get very big.

  • 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-27T00:46:23+00:00Added an answer on May 27, 2026 at 12:46 am

    The iostream classes take care of all necessary buffering, so you don’t
    have to. The usual idiom to copy an entire file is just:

    fout << fin.rdbuf();
    

    iostream takes care of all of the necessary buffering. (This is a
    somewhat unusual use of <<, since it doesn’t format. Historical
    reasons, no doubt.)

    If you need the loop, perhaps because you want to do some
    transformations on the data before rewriting it, then it’s a little
    tricker, since istream::read “fails” unless it reads the
    requested number of characters. Because of this, you have to also check
    how many characters were read, and process them even if the read failed:

    int readCount;
    while ( fin.read( &buf[0], buf.size() )
            || (readCount = fin.gcount()) != 0 ) {
        //  ...
        fout.write( &buf[0], readCount );
    }
    

    This is fairly ugly; a better solution might be to wrap the buffer in a
    class, and define an operator<< for this class.

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

Sidebar

Related Questions

in my application, i'm trying to achieve something like this: i have: data 0,
I've trying to achieve something like this: class Base { public: Base(string S) {
What I am trying to achieve is something like this: class object: def __init__(self):
Trying to achieve something like this using recursion: if (m > n) return; Foo
I'm trying to achieve something like this. Any ideas? function test(that){ $(that).blur(); } $(#form).focus(test(this));
I'm trying to achieve something like this NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@A,@B,@C, nil]; NSLog(@Array:
Been trying this for quite a while now and I need help. Basically I
I've been trying to find the best way to do this for a while
I'm trying to build XmlDocument so that after serialization I could achieve something like
I'm trying to achieve something google isn't able to give answer. I'm trying to

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.