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

The Archive Base Latest Questions

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

I have a binary file (test.bin) which has 2 unsigned int values respectively 1000

  • 0

I have a binary file (test.bin) which has 2 unsigned int values respectively 1000 and 4000 in it.
With the below code, I want to change the first number to 5000 with the first write function and then I want to read second number and rewrite it with 4000-2000 = 2000.
However, the program changes 1000 to 5000 but it does not change 4000 to 2000. Even though I use file.flush() or file.sync(), it does not have any effect.
Interestingly, when I put file.tellg() or file.tellp(), it works as I want. (I found it out by coincidence)
This happens both on Linux and Windows. On Linux I try to compile it with g++.
sizeof(unsigned int)=4 and I’m sure program can open test.bin.

#include <fstream>
#include <iostream>

using namespace std;

int main(){
    fstream file;
    unsigned int data, buffer;
    data=5000;
    file.open("test.bin", ios::binary | ios::in | ios::out);

    file.write((char*)&data,4); // will change first number to 5000

    // file.flush();  // Nothing changes if I delete comment signs.

    // file.tellp();  // Program works correctly if I uncomment this. 
    // file.tellg();  // Program works correctly if I uncomment this.

    file.read((char*)&buffer, 4);  // position pointer should be at the beginning of the 2nd number

    file.seekp(-4, ios::cur); // Since internal pointer is at the end of the file after the read(), I manually put it back to the beginning of the 2nd number.

    buffer-=2000;
    file.write((char*)&buffer,4); // Now, it should rewrite 2nd number with 2000. 

    file.close();
    return 0;
}
  • 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-27T03:29:23+00:00Added an answer on May 27, 2026 at 3:29 am

    Bingo! You have stumbled upon a very obscure, almost unwritten and at best ambiguous requirement of the C++ language. I tried to argued for removing it from G++, and wrote a patch to do so, but met a lukewarm response from the maintainers which was accepted after I showed it could be done without a heavy redesign.

    If memory serves, the C++ specification mentions that file streams work with the same semantics as C <stdio.h> streams, and the C spec says that you must seek between reading and writing to the same file.

    Here is the bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45708

    Discussion #1: http://gcc.gnu.org/ml/libstdc++/2010-09/msg00079.html

    Discussion #2: http://gcc.gnu.org/ml/libstdc++/2010-09/msg00104.html

    EDIT: I need a better memory! Actually that was committed on September 22, 2010, into GCC 4.6.0. The remaining edits which weren’t committed related to changing the locale codecvt facet during I/O.

    The bug tracking the accepted changes: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45628

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

Sidebar

Related Questions

I have a program written in C++, that opens a binary file(test.bin), reads it
I have the below Perl code to make a copy of a binary file
I have a binary file of size about 5MB.. which has lots of interspersed
I have a binary file to which I want to append a chunk of
I have a binary file to which I'm trying to write however I dont
We have a binary file from which we have identified the following dates (as
Say I have a binary file (generated with Java) containing a 32 bit int
If I have a large binary file (say it has 100,000,000 floats), is there
I have an ASCII binary file which looks something like: 00010110001001000110011001000111 01011000011100001010100001001000 11110001011010000010010101111010 00000000000000000000000000000000
We have a huge binary and which has constants defined in namespace ex say

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.