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

  • Home
  • SEARCH
  • 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 7694207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:16:00+00:00 2026-05-31T21:16:00+00:00

I have a program shown as follows. For it I have several questions: 1).

  • 0

I have a program shown as follows. For it I have several questions:

1). Why does it produce different results on different platforms? I’ll paste the screen-shots later.

2). I’m using a fail() method to check if the “file.read()” failed. Is this correct? I use fail() method because this web page says this:

The function returns true if either the failbit or the badbit is set. At least one of these flags is set when some error other than reaching the End-Of-File occurs during an input operation.

But later I read this page about istream::read() here. It says the eofbit and failbit would always be set at the same time.. Does this mean that a normal EOF situation would also result in that fail() returns true? This seems to conflict with “other than reaching the End-Of-File occurs”..

Could anyone help me clarify how I am supposed to use these methods? Should I use bad() instead?

My program

#include <iostream>
#include <fstream>
using namespace std;

#ifdef WIN32
char * path="C:\\Workspace\\test_file.txt";
#else
char * path="/home/robin/Desktop/temp/test_file.txt";
#endif

int main(int argc, char * argv[])
{
    ifstream file;

    file.open(path);
    if (file.fail())
    {
        cout << "File open failed!" << endl;
        return -1;  // If the file open fails, quit!
    }

    // Calculate the total length of the file so I can allocate a buffer
    file.seekg(0, std::ios::end);
    size_t fileLen = file.tellg();
    cout << "File length: " << fileLen << endl;
    file.seekg(0, std::ios::beg);

    // Now allocate the buffer
    char * fileBuf = new (std::nothrow) char[fileLen+1];
    if (NULL == fileBuf)
        return -1;
    ::memset((void *)fileBuf, 0, fileLen+1);    // Zero the buffer

    // Read the file into the buffer
    file.read(fileBuf, fileLen);
    cout << "eof: " << file.eof() << endl
         << "fail: " << file.fail() << endl
         << "bad: " << file.bad() << endl;
    if (file.fail())
    {
        cout << "File read failed!" << endl;
        delete [] fileBuf;
        return -1;
    }

    // Close the file
    file.close();

    // Release the buffer
    delete [] fileBuf;

    return 0;
}

The test_file.txt content(shown with “vim -b”. It’s a very simple file):

test_file.txt content

Result on Windows(Visual Studio 2008 SP1):

Windows result

Result on Linux(gcc 4.1.2):

Linux result

  • 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-31T21:16:02+00:00Added an answer on May 31, 2026 at 9:16 pm

    Does this mean that a normal EOF situation would also result in that fail() returns true? This seems to conflict with “other than reaching the End-Of-File occurs”.

    I recommend using a reference that isn’t full of mistakes.

    http://en.cppreference.com/w/cpp/io/basic_ios/fail says:

    Returns true if an error has occurred on the associated stream. Specifically, returns true if badbit or failbit is set in rdstate().

    And the C++ standard says:

    Returns: true if failbit or badbit is set in rdstate().

    There’s no “other than end-of-file” thing. An operation that tries to read past the end of the file, will cause failbit to set as well. The eofbit only serves to distinguish that specific failure reason from others (and that is not as useful as one might think at first).


    I’m using a fail() method to check if the “file.read()” failed. Is this correct?

    You should simply test with conversion to bool.

    if(file) { // file is not in an error state
    

    It’s synonymous with !fail(), but it’s more usable, because you can use it to test directly the result of a read operation without extra parenthesis (things like !(stream >> x).fail() get awkward):

    if(file.read(fileBuf, fileLen)) { // read succeeded
    

    You will notice that all read operations on streams return the stream itself, which is what allows you to do this.


    Why does it produce different results on different platforms?

    The difference you’re seeing between Windows and Linux is because the file is open in text mode: newline characters will be converted silently by the implementation. This means that the combination "\r\n" (used in Windows for newlines) will be converted to a single '\n' character in Windows, making the file have only 8 characters. Note how vim shows a ^M at the end of the first line: that’s the '\r' part. In Linux a newline is just '\n'.

    You should open the file in binary mode if you want to preserve the original as is:

    file.open(path, std::ios_base::in | std::ios_base::binary);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program shown below: Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); EndPoint
I have a Enumeration as shown in below program public class Test { public
I have a C++ program. It's quite simple - shows an image (splash screen)
I have a program that I want to either hide or show certain UIbuttons
I have a progress bar to show the status of the program loading songs
I have program that has a variable that should never change. However, somehow, it
I have program, that must interact with a console program before my program can
I have program that runs fast enough. I want to see the number of
I have a DOS command which outputs as follows (just an example containing 3
I have a MySQL query as follows: SELECT KeywordText, SUM(Frequency) AS Frequency FROM Keyword,

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.