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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:00:28+00:00 2026-06-01T15:00:28+00:00

I am trying to learn C++ fstream, ifstream, ofstream. On Half way through my

  • 0

I am trying to learn C++ fstream, ifstream, ofstream. On Half way through my project I learnt that if we are accessing the same file with ofstream and ifstream for read & write, its better to close the streams, before using another.

Like
ofstream write_stream(…);
ifstream read_stream(….);

 // Accessing pointers using both read_stream, write_stream interchangebly 

  read_stream.read(....);
  write_stream.write(....);

  read_stream.close(); 
  write_stream.close();

///

In the above case, I guess both the stream use the same pointer in the file, so I need to be aware of the pointer movement, so I have to seek each and every time I try to read() or write().

I guess, I am right, so far.

To avoid any more confusion, I have decided to use this format

 fstream read_write_stream("File.bin",ios::in|ios::out|ios::binary);
 read_write_stream.seekp(pos);
 read_write_stream.seekg(pos);
 read_write_stream.tellp();
 read_write_stream.tellg()

 read_write_stream.read(...);
 read_write_stream.write(...);

 read_write_stream.close()

Is there any bug inducing feature, that I should be aware of in the above program??? Please advice

  • 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-01T15:00:30+00:00Added an answer on June 1, 2026 at 3:00 pm

    Though I don’t know if the standard explicitly refers to this case, I don’t think that a C++ compiler can promise you what will happen in the case where you use several streams to change the same external resource (file in this case). In addition to the fstream internal implementation, it depends on the OS and the hardware you’re writing to. If I’m correct, it makes this operations, by default, undefined behavior.

    If you use two different streams, most likely each will manage its own put and get pointer, each will have buffering which means that if you don’t use flush(), you won’t be able to determine what is the order the operations will be done on the file.

    If you use one stream to both read and write, I think the behavior will be predictable and more trivial understand.

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

Sidebar

Related Questions

Trying to learn C. Want to read the first line of a text file,
Trying to learn a bit about PDO and is going through this tutorial .
I'm trying to learn WCF and I've created a new WCF Service Library project
Trying to learn PL/SQL with multimedia data. Can anyone please point out that from
Trying to learn jquery here so I took a regular javascript snippet that loops
I am trying learn how to work with CEDET. I don't have any project
I trying to learn BDD way for development and just watched RailsCasts lesson for
Trying to learn HTML5 and downloaded a test project https://github.com/amiklosi/Comicr . There is a
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
Trying to learn C++ and working through a simple exercise on arrays. Basically, I've

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.