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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:52:09+00:00 2026-05-23T18:52:09+00:00

The code below will print all of the text from the sample text file

  • 0

The code below will print all of the text from the sample text file I’m using except for the last little snippet of it. I think this has something to do with the eof or the byte size I’m using not working as I expect.

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]){
int length;
char* buffer;

//get file stream and open local file.
ifstream stream;
stream.open("SampleFile.txt", ios::binary);
stream.seekg(0, ios::end);
length = stream.tellg();
stream.seekg(0, ios::beg);

//read stream 1024 bytes at a time until all bytes of file are used
buffer = new char[1024];
bool eof = false;
while(!eof)
{
    stream.read(buffer, 1024);
    cout.write(buffer, 1024);
    if(stream.eof())
        eof = true;
    //cout << i << endl;
    //cout.write(buffer, 1024);
}

stream.close();
delete[] buffer;
return 0;
}

What am I missing?

  • 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-23T18:52:10+00:00Added an answer on May 23, 2026 at 6:52 pm

    As you already know, you have a wrong size of buffer. The other thing is the read of less than 1024 characters (going to happen at the end if your file doesn’t have exactly n*1024 bytes). Take the advantage of istream::gcount which gives you number of characters extracted by last read:

    char buffer[1024];
    while(stream)
    {
        stream.read(buffer, 1024);
        cout.write(buffer, stream.gcount());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser
Which code snippet will give better performance? The below code segments were written in
I have python code below that will loop through a table and print out
Greetings, The VBA code below will create an Excel QueryTable object and display it
Will the code below work if the clock on the server is ahead of
in the code below at first if statements block (there will be more than
I'm doing a homework project that requires this: Below you will find the code
The code below shows a sample that I've used recently to explain the different
I'm using the code from this link to send attachments in the email notifications
I have an SQL file which will give me an output like below: 10|1

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.