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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:57:23+00:00 2026-05-23T16:57:23+00:00

I want to read in some data from a file. Say an integer: fread(&var1,

  • 0

I want to read in some data from a file. Say an integer:

fread(&var1, 4, 1, f);

Where var1 would be an integer. But then I got to thinking that this is not safe as there isn’t any guarantee that an integer is 4 bytes long. (I’m ignoring other issues like feof and ferror for the sake of this question).

I also soon realised that there were even more issues than just int size, such as the endianness of the system, and probably others which I haven’t even thought of.

So, what is the best way to ensure that you data being read in is interpreted properly? So far, the only thing I can think of is to just store the data as text rather than as binary data, read in the text, and convert it at run time. I would guess that no matter the solution though, if you wanted to ensure that it is portable, it would always involve some form of conversion anyway.

Thank you.

  • 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-23T16:57:23+00:00Added an answer on May 23, 2026 at 4:57 pm

    To avoid the size problem, you should be doing:

    fread(&var1, sizeof(var1), 1, f);
    

    If you’re worried that the size of int might vary between the platform that writes the data and the platform that reads the data, then you have a more fundamental problem. In this scenario, you should avoid using int, short, etc., and use the types defined in <stdint.h>, such as int16_t, uint32_t.

    To deal with endianness issues, you should consider writing helper functions that explicitly write/read the individual bytes in a known order, such as:

    void write_uint32_t(uint8_t *buf, uint32_t x)
    {
        buf[0] = (uint8_t)(x >> 0);
        buf[1] = (uint8_t)(x >> 8);
        buf[2] = (uint8_t)(x >> 16);
        buf[3] = (uint8_t)(x >> 24);
    }
    

    All of the above applies only to integer types. For floating-point types, there is no perfect universal solution.

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

Sidebar

Related Questions

I am new to C, and want to read some data from a file.
I want to read data from a YAML file but I need the order
In my grails application I want to read some values from properties file and
I want to read and save some data from this rss feed to in
I want to read some data from a device connected to a COM port.
I want to read data from a CSV file using microsoft text driver. Can
I want to read N bytes of data from a file stream and append
In my android project, i want to read a data structure from file. A
In our web application we want to get some data from a text file
I want to do some basic filtering on a file. Read it, do processing,

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.