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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:44:30+00:00 2026-06-14T10:44:30+00:00

Im trying to write and read an array of ints in a binary file.

  • 0

Im trying to write and read an array of ints in a binary file. The two functions look like this, more or less.

savefunction

int numbers[6]={0, 2, 3, 3, 0, 1};
FILE *file;
if(file=fopen(filename, "wb")==NULL)
{
    printf("Something went wrong reading %s\n", filename);
    return 0;
}
else
{
    int i;
    for(i=0; i<6; i++)
        fprintf(file, "%d", numbers[i]);
}
fclose(file);

loadfunction

FILE *saved_data;
int errors=0;
if((saved_data=fopen(filename, "rb"))==NULL)
    errors++;
else
{
    fread(first, sizeof(int), 1, saved_data);
    fread(second, sizeof(int), 1, saved_data);
    fread(third, sizeof(int), 1, saved_data);
    fread(fourth, sizeof(int), 1, saved_data);
    fread(fifth, sizeof(int), 1, saved_data);
    fread(sixt, sizeof(int), 1, saved_data);
}
fclose(saved_data);

Now when I debug the program the debugger tells me that the first element is the following

(gdb) print first
$1 = (int *) 0x7fff5fbff968
(gdb) print *first
$2 = 858993200

I can’t understand this. The file when opening with editor says 023301

  • 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-14T10:44:31+00:00Added an answer on June 14, 2026 at 10:44 am

    The complementary function of fread is fwrite, not fprintf. What you’re doing with fprintf is putting the integers into the file as text, not as binary.

    Also, make sure you pass a pointer to fread, not an integer. You don’t show the declarations of first, second, and so on, but if you’ve declared something like

    int first, second, third, fourth, fifth, sixt;
    

    then you’ll need to use code like

    fread(&first, sizeof(int), 1, saved_data);
    

    rather than

    fread(first, sizeof(int), 1, saved_data);
    

    Since your integers are already in an array, you can just write the array in one go with

    fwrite(numbers, sizeof(int), 6, file);
    

    and avoid the for loop altogether.

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

Sidebar

Related Questions

I am trying to write 2 functions, one to read the matrix (2D array)
I'm trying to write/read a double matrix to file as binary data but I'm
I'm trying to write and then read a variable to a file. The variable
I am trying to write some simple code which will read a text file
i have been trying to write a program in python to read two text
I have been trying to write some code in Scala to read a file
I am trying to write some code that will open a file, read its
I'm trying to write some puppet .erb, I'd like to handle this environment variable
I am trying to write a class in Haxe supporting array like access using
I'm trying to write an array (list?) to a text file using Python 3.

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.