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

  • Home
  • SEARCH
  • 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 8941331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:16:32+00:00 2026-06-15T11:16:32+00:00

This might already have been discussed, but I couldn’t find any related topic. I

  • 0

This might already have been discussed, but I couldn’t find any related topic.

I want to read short values from a file and store them in a buffer of int values using fread. So basically I want to do this:

  int *data; //int buffer
  data = (int *) malloc(1000 * sizeof(int));
  fread(data, sizeof(short), 1000, infile); //infile is organized in 2-byte chunks

Data are stored in a file in 2-byte chunks (short). But when I read those data, I want to put them into an int buffer. How can I do that without copying from a short buffer to an int buffer? fread gets void pointer, so it doesn’t care about the type of buffer.

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

    The fread function doesn’t know about types, that’s correct. That’s why it cant read raw bytes and convert them, it just puts them as-is into the provided buffer. And as you know, a short on most platforms is two bytes while int is four. So that will not work as fread has not idea about that.

    You either read one short at a time, putting it into the int buffer, or read into a temporary short buffer then loop and copy value by value into the int buffer.

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

Sidebar

Related Questions

I know this question might have already been discussed a thousands of times but
I know this question might have been asked a few times already, but I
You might think that this question is already answered. However, I couldn't find the
This might have to be broken down a bit but what i want to
this question might have been answered before, but after two days of searching I
This may already have been asked, but StackOverflow is massive and trying to google
This might be a simple question but I've searched and searched and can't find
I asked this same question about a week ago, but I might not have
I know this might ma a simple question, it must have been answered before
This might seem like a very easy question for some of you folks, but

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.