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

The Archive Base Latest Questions

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

In my project, there is a need to read and write to binary file,

  • 0

In my project, there is a need to read and write to binary file, basically serializing linked lists in a file, where I store the value in binary format and remember the tellp()/tellg() offset, how ever, I cannot do that. That erases all the contents in it to zero and instead of inserting it pushes the current content back.

For Example, in the below program, I open a file, write values say 1,120,323. Then close it and read it, it shows the exact correct values 1,120,323. But When I try to replace the value of 120->220, 1 becomes zero and the value read as 0 220 220. Basically 220 over writes and pushes 323 back.

#include <iostream>
#include <fstream>
#include <cstdlib>

int main() {
  std::cout<<"File Ofstream Testing "<<std::endl;   

  const char * file_name = "Test_File.bin";
  int ONE = 1;
  int ZERO = 0;

  int ONE_TWENTY = 120;
  int TWO_TWENTY = 220;

  int THREE_TWENTY_THREE = 323;
  int THREE_FORTY_FIVE = 345;

//---------------------------------------------------------------------------
{
 std::ofstream file_write(file_name, std::ios::out|std::ios::binary);

 if(!(file_write.is_open())) {
    std::cout<<"File cannot be opened "<<std::endl;
    exit(0);
 }

 file_write.seekp(0);
 file_write.write((char *)&ONE,sizeof(int));
 file_write.write((char *)&ONE_TWENTY,sizeof(int));
 file_write.write((char *)&THREE_TWENTY_THREE,sizeof(int));

 file_write.close();
}
//---------------------------------------------------------------------------
{
 std::ifstream file_read(file_name,std::ios::in|std::ios::binary);

 if(!(file_read.is_open())) {
        std::cout<<"File cannot be opened "<<std::endl;
        exit(0);
 }

 int temp;
 file_read.seekg(0);
 file_read.read((char *)&temp,sizeof(int));
 std::cout<<"Temp "<<temp<<std::endl;
 file_read.read((char *)&temp,sizeof(int));
 std::cout<<"Temp "<<temp<<std::endl;
 file_read.read((char *)&temp,sizeof(int));
 std::cout<<"Temp "<<temp<<std::endl;

 file_read.close();
}
  //---------------------------------------------------------------------------
{
  std::ofstream file_write(file_name, std::ios::out|std::ios::binary);

  if(!(file_write.is_open())) {
   std::cout<<"File cannot be opened "<<std::endl;
   exit(0);
 }

 file_write.seekp(sizeof(int));
 file_write.write((char *)&TWO_TWENTY,sizeof(int));
 file_write.seekp(sizeof(int) + sizeof(int));
 file_write.write((char *)&THREE_FORTY_FIVE,sizeof(int));

 file_write.close();
}
//--------------------------------------------------------------------------
{
  std::ifstream file_read(file_name,std::ios::in|std::ios::binary);

  if(!(file_read.is_open())) {
    std::cout<<"File cannot be opened "<<std::endl;
    exit(0);
}

  int temp;
  file_read.seekg(0);
  file_read.read((char *)&temp,sizeof(int));
  std::cout<<"Temp "<<temp<<std::endl;
  file_read.seekg(sizeof(int));
  file_read.read((char *)&temp,sizeof(int));
  std::cout<<"Temp "<<temp<<std::endl;
  file_read.seekg(sizeof(int) + sizeof(int));
  file_read.read((char *)&temp,sizeof(int));
  std::cout<<"Temp "<<temp<<std::endl;

   file_read.close();
 }
 //--------------------------------------------------------------------------------
 return 0;
}//fn:main
  • 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:37:35+00:00Added an answer on June 1, 2026 at 3:37 pm

    For output streams, opening with std::ios::out is equivalent to std::ios::out | std::ios::trunc, so when you declare std::ofstream file_write for the second time, the previously written content gets discarded and you’re writing the file anew. When you then do file_write.seekp(sizeof(int)); on an empty stream, zero bytes are written.
    If you needed to append to an ofstream then you would open it with std::ios:app. That wouldn’t truncate the file, but it would only let you append to it on the other hand.

    If you want to do random-access writing on an file stream, you need to declare it as fstream and open it in both reading and writing mode. So what you need is:

    std::fstream file_write(file_name, std::ios::in | std::ios::out | std::ios::binary);

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

Sidebar

Related Questions

In our project we need to import the csv file to postgres. There are
For a project I need to read and write ESRI shapefiles. It should be
In an Excel VBA project, I need to read and write UTF-8-encoded text files.
I am working on one project where there is a functionality need to implement
There is a project that I need to maintain that talks to mysql via
i need to write simple android application that run on the background and read
I've got an embedded Linux project where I need to read video in through
In my project there are large no. of FIXME / TODO which are to
In my project there are some 'Prototype' factories that create instances by cloning a
I am using My-eclipse and doing a struts project there is no syntax error

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.