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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:12:10+00:00 2026-06-17T11:12:10+00:00

I am trying to write data to a binary file and then read back

  • 0

I am trying to write data to a binary file and then read back the data from the file. The data consists of a single number (an integer) nrows. Below is my code for writing the data to a binary file. However, when I try to read back the data and print the result using printf, I obtain a nonsensical result: -2.

FILE *fout;
FILE *file_pointer;
int nrows = 5;

fout = fopen("matrixB.bin", "wb") //Writing to a binary file.//

fwrite(&nrows, sizeof(int), 1, fout); //Writing the number nrows to the binary file "matrixB.bin"//

file_pointer = fopen("matrixB.bin", "rb"); //Reading a binary file.//

fread(&nrows, sizeof(int), 1, file_pointer);

printf("%d", nrows); //Here -2 is printed, instead of 5.//

What is the problem with my code?

  • 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-17T11:12:12+00:00Added an answer on June 17, 2026 at 11:12 am

    One of the characteristics of the C file functions (fread, fwrite, etc) is that they normally perform buffered I/O.

    Your program doesn’t flush and/or close the file before reopening it. It should.

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

Sidebar

Related Questions

In my program, I am trying to read data from binary file, and then
I'm trying to read binary data from a specific offset. I write the data
I'm trying to write code to read a binary file into a buffer, then
I am trying to write data stored in a binary file using turbo C++.
I want to write data into the file in binary form. I was trying
I am trying to write my spatial data from a table to a file.
I'm trying to write binary data files from fortran, but I find the regular
I am trying to write a lot of data into a binary file. Because
Im trying to write and read an array of ints in a binary file.
I am trying to write data to a binary file using the Print() method,

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.