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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:17:55+00:00 2026-06-09T02:17:55+00:00

Whenever I want to read or write into a binary file using C, I

  • 0

Whenever I want to read or write into a binary file using C, I do use the fread() and fwrite() functions. They need as a parameter the bytes of the datum that is being read or written so I use the sizeof() function. Now the question is:

The books says that I should declare a function like this:

fread(&variable,sizeof(TYPE_OF_VAR),quantity,file);

I’ve use the following statement which works most of the time but not always:

fread(&variable,sizeof(VARIABLE),quantity,file);  

Why does it works sometimes but sometimes it doesn’t?
Does it depends on the type of the variable (int, char, etc)?
Does it depends on the quantity of the datum that I use?

  • 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-09T02:17:57+00:00Added an answer on June 9, 2026 at 2:17 am

    The thing to keep in mind is that sizeof is based on what the compiler knows about the type at compile time (ignoring VLA’s for now). If you give it a variable, it will use the type of that variable.

    So, the only time I can think of where it wouldn’t work as you expect is with pointers.

    basically what it boils down to is this:

    int x[5];
    int *y = &x[0];
    int *p = malloc(sizeof(int) * 5);
    sizeof(x); // == sizeof(int) * 5
    sizeof(y); // == sizeof(int *)
    sizeof(p); // == sizeof(int *)
    

    This gets tricky when dealing with functions because arrays decay to pointers when passed to a function. Also note that all 3 of these are exactly equivalent:

    int func(int *p);
    int func(int p[5]);
    int func(int p[]);
    

    in all 3, p is a pointer, not an array.

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

Sidebar

Related Questions

Whenever I want to modify a winform from another thread, I need to use
Whenever I want to get data from a plist file I use the following
I want to write a simple java program to read in a text file
I want to write a c program that keeps reading a file and whenever
i'm using the DOMdocument class in php whenever i want to create a XML
As a PHP developer, I always use the code below whenever I want to
I want to gzip a file hold it in memory and whenever a request
I want to include a static log file within my app. Whenever the user
I am having trouble with Lisp's backquote read macro. Whenever I try to write
I am using the java properties file construct. At the beginning I read it

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.