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

The Archive Base Latest Questions

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

I am writing a binary file in C++, where I traverse to a particular

  • 0

I am writing a binary file in C++, where I traverse to a particular location and need to over-write the contents with the new one. I know the contents of the integer size, so I keep track of the location and over write it. Seems like, ofstream write function inserts or appends the variable. Is there a way, where I can over write, instead of appending or inserting in a file

  long word = sizeof(int) + sizeof(long) + sizeof(long);

 std::ofstream file_write(filename, std::ios::out|std::ios::binary);

 file_write.seekp(word);      
 long pos = file_write.tellp();
 file_write.write((char *)&some_integer,sizeof(int));
 file_write.close()

 //some other location

 file_write.seekp(pos);
 file_write.write((char *)&some_other_integer,sizeof(int));

//Here some_integer should be over written by some_other_integer. Is there a way to do it?

That will be like

value1,value2,value3

I might be able to replace value 2 with value 5.

then it should look like

value1,value5,value3

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

    You need to add std::ios::trunc to your open parameters, as in:

    std::ofstream file_write(filename, std::ios::out|std::ios::binary|std::ios::trunc);
    

    That will truncate the file, discarding any existing content.

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

Sidebar

Related Questions

In java I need to read a binary file from a site and write
I'm writing a Binary file converter in which I need to convert 1-6 byte
I am writing a php program to write a binary file (may be video
I'm writing a cross-platform package which need to include different binary file for different
I am writing to binary file using fstream and when open the file using
I am writing the below structure in to the file using Binary Writer. File
At a previous employer, we were writing binary messages that had to go over
I am using fopen to write to a binary file and using the cstdio
I'm getting data from a binary file, reading from file and writing in a
I am trying to figure out how to write a binary file with a

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.