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

The Archive Base Latest Questions

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

I am trying to overwrite a single byte in a binary file. I am

  • 0

I am trying to overwrite a single byte in a binary file. I am using fseek() to set the stream position indicator to the byte that I want to overwrite. Then I am calling to fwrite() with a new byte in that location. Somehow the other bytes in the file are being affected. Heres some code:

bool x = 1;
bool y = 0;
bool z = 1;

/* WRITE 101 to file */
FILE *ff = fopen(file, "wb");
fwrite(&x, sizeof(x), 1, ff);
fwrite(&y, sizeof(y), 1, ff);
fwrite(&z, sizeof(y), 1, ff);

fclose(ff);

bool x_r, y_r, z_r;

/* READ from file */
ff = fopen(file, "rb");
fread(&x_r, sizeof(x_r), 1, ff);
fread(&y_r, sizeof(y_r), 1, ff);
fread(&z_r, sizeof(z_r), 1, ff);

fclose(ff);

/* PRINT to standard output */
cout << x_r << y_r << z_r << endl; // OUTPUT: 101

bool overwrite = 1;

/* OVERWRITE */
ff = fopen(file, "wb");

fseek(ff, 1, SEEK_SET);
fwrite(&overwrite, sizeof(overwrite),1 , ff);
fclose(ff);


/* READ from file */
ff = fopen(file, "rb");
fread(&x_r, sizeof(x_r), 1, ff);
fread(&y_r, sizeof(y_r), 1, ff);
fread(&z_r, sizeof(z_r), 1, ff);

fclose(ff);

/* PRINT to standard output */
cout << x_r << y_r << z_r << endl; // OUPUT 011; I was expecting 111 

The above code basically writes three bytes of data to a file. 101. Then I use fseek() to set to reposition the stream indicator to point to the second byte. But calling fwrite() to overwrite the second byte from 0 to 1 is changing the first byte as well. The expected output is 111, but I am getting 011.

Any ideas why this is happening? I tried putc as well, but that didn’t work out either. Any help would be appreciated.

  • 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:54+00:00Added an answer on May 27, 2026 at 3:29 am

    “r+” mode opens the file for reading and writing, but preserves its contents.

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

Sidebar

Related Questions

I have an application where I am trying to implement a safe file overwrite
I have a single thread trying to connect to a database using JDBCTemplate as
I'm trying to request a single location update using the new LocationManager.requestSingleUpdate() method, but
Good evening. I'm trying to map a class hierarchy to a single table using
I'm trying to overwrite certain pieces in kernel memory (Linux 2.6.31, Ubuntu 9.10) in
I have an existing document in a document library and I'm trying to overwrite
I'm trying to override Ant compiler attributes via the command line so that all
I’m trying to prevent the browser from using the :hover effect of the CSS,
I'm trying to create related articles for our news, based on tags (single word
I am trying to create an Alert Dialog with a list of single-choice items

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.