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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:46:53+00:00 2026-05-24T18:46:53+00:00

I encountered a problem and my current knowledge of C++ is not enough to

  • 0

I encountered a problem and my current knowledge of C++ is not enough to solve it. I looked for the answer in books of Stroustrup, but a full understanding of what I’m doing wrong for me not now.

So the essence.

I write to the file:

int i = 1;
int j = 2;
ofstream ofs("file", ios::binary);
ofs.write(as_bytes(i), sizeof(int));
ofs.write(as_bytes(j), sizeof(int));

After that, I need to update the second value:

int j = 10;
ofstream ofs("file", ios::binary);
ofs.seekp(4, ios::beg);
ofs.write(as_bytes(j), sizeof(int));

And when I try to read the file:

int i = 0;
int j = 0;
ifstream ifs("file", ios::binary);
ifs.read(as_bytes(i), sizeof(int));
ifs.read(as_bytes(j), sizeof(int));
cout << i << ' ' << j << endl;

It turns out that I lose the first value. What am I doing wrong? Why did it disappear?

  • 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-24T18:46:54+00:00Added an answer on May 24, 2026 at 6:46 pm

    By default the file will be truncated (ios:trunc, i.e. the content is lost upon opening the file for writing).

    For the second write operation explicitly add the flags ios:in AND ios:out despite the fact you’re writing only. So essentially I’d use the following:

    ofstream ofs("file", ios::binary | ios::in | ios::out | ios::ate);
    

    This should open the file with the stream/file pointer being at the end of the file (ios::ate might be optional though).

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

Sidebar

Related Questions

I've just encountered a problem that my current (almost equal to 0) CSS knowledge
I have encountered a problem that I have not come accross yet when setting
We encountered a problem with using Subversion on Windows. A developer committed a file
I encountered a problem when running some old code that was handed down to
I encountered a problem dealing with UTF-8, XML and Perl. The following is the
I recently encountered a problem where a value was null if accessed with Request.Form
I've encountered a problem when retrieving a JSONP response from a server in a
I've encountered a problem with my Netbeans 6.1 IDE. After an unsuccessful update, Netbeans
I recently encountered a problem with my Profile provider: it wouldn't retrieve profiles correctly
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to

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.