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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:15:49+00:00 2026-06-11T14:15:49+00:00

For performing file IO in C++ we use the ofstream, ifstream and fstream classes.

  • 0

For performing file IO in C++ we use the ofstream, ifstream and fstream classes.

  • ofstream: Stream class to write on files
  • ifstream: Stream class to read from files
  • fstream: Stream class to both read and write from/to files

The process of associating a file with a stream object is called “opening the file”.
When opening a file we can specify the mode in which the file is to be opened.
My query is related to the ios::out and ios:in modes.

When I create an ofstream object and open the file with ios::in mode, I am able to
write into the file but only if its created already(with ios::out mode file is also created if it doesn’t already exist).

But when I create ifstream object and open the file with ios::out mode, I am able to read from the file.

My question is why these modes (ios::in/ios::out) are supplied by the language when the type of the stream(ifstream/ofstream) itself specifies as to which type of operation(input/output) is being performed ?

Also why this ambiguous usage(ofstream with ios::in and ifstream with ios::out) works in one case and fails(though only if file is not already present) in another ?

  • 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-06-11T14:15:50+00:00Added an answer on June 11, 2026 at 2:15 pm

    The ofstream, ifstream and fstream classes are high level interfaces for the underlying filebuf, which one can access through the rdbuf() member function of the stream.

    When you open an ofstream with some mode mode, it opens the underlying stream buffer with mode | ios_base::out. Analogously ifstream uses mode | ios_base::in. fstream passes the mode parameter verbatim to the underlying stream buffer.

    What the above implies is that the following code opens the file with exactly the same open flags:

    fstream f("a.txt", ios_base::in | ios_base::out);
    ifstream g("a.txt", ios_base::out);
    ofstream h("a.txt", ios_base::in);
    

    After these lines you can do exactly the same things with f.rdbuf(), g.rdbuf() and h.rdbuf(), and all the three act as if you opened the file with the C call fopen("a.txt", "r+"), which gives you read/write access, does not truncate the file, and fails if the file does not exist.

    So, why do we have three different classes? As previously stated, these are high level classes providing a high-level interface over the lower-level stream buffer. The idea is that ifstream has member functions for input (like read()), ofstream has member functions for output (like write()) while fstream has both. For example you cannot do this:

    g.write("abc", 3); // error: g does not have a write function
    

    But this works, because, although g is an ifstream, we opened it with ios_base::out:

    g.rdbuf()->sputn("abc", 3); // we still have write access
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am performing a scattered read of 8-bit data from a file (De-Interleaving a
i want to read\write a binary file which has the following structure: The file
I am creating a file to read in a certain number of .wav files,
I have a 12 XML files from which I am extracting ONE CSV file,
To use code I have written for performing calculations, I need to read in
I am performing a stencil computation on a matrix I previously read from a
I trying to write a program that navigates the local file system using a
Calling fclose() here after dup() ing its file descriptor blocks until the child process
A quote from Effective Java states that: A second legitimate use of finalizers concerns
I am a complete novice in C++. I am trying to read a file

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.