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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:19:30+00:00 2026-05-29T10:19:30+00:00

Hi in my project I’ve to read a .bin file which has sensor data

  • 0

Hi in my project I’ve to read a .bin file which has sensor data in the form of short(16 bit values). I’m doing this using fread function into a buffer, but I feel that the reading-in is not happening correctly. I mean there is no consistence between what I write and what I read in. Can you guys suggest what is going wrong here? This is not my code from my project… I’m only trying to verify the fread and fwrite functions here.

#include<stdio.h>
void main()
{
    FILE *fp = NULL;

    short x[10] = {1,2,3,4,5,6,5000,6,-10,11};
    short result[10];

    fp=fopen("c:\\temp.bin", "wb");

    if(fp != NULL)
    {
        fwrite(x, 2 /*sizeof(short)*/, 10 /*20/2*/, fp);
        rewind(fp);
        fread(result, 2 /*sizeof(short)*/, 10 /*20/2*/, fp);
    }
    else
        exit(0);

    printf("\nResult");
    printf("\n%d",result[0]);
    printf("\n%d",result[1]);
    printf("\n%d",result[2]);
    printf("\n%d",result[3]);
    printf("\n%d",result[4]);
    printf("\n%d",result[5]);
    printf("\n%d",result[6]);
    printf("\n%d",result[7]);
    printf("\n%d",result[8]);
    printf("\n%d",result[9]);

    fclose(fp)
 }

After I do the fread() (HEX values):

temp.bin:
01 02 03 04 05 06 e1 8e 88 06 ef bf b6 0b...

After I do the fwrite()

stdout:
Result
0
914
-28
-28714
-32557
1
512
-32557
908
914
  • 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-29T10:19:31+00:00Added an answer on May 29, 2026 at 10:19 am

    Open the file with mode w+ (reading and writing). The following code works:

    #include<stdio.h>
    int main()
    {
        FILE *fp = NULL;
    
        short x[10] = {1,2,3,4,5,6,5000,6,-10,11};
        short result[10];
        int i;
    
        fp=fopen("temp.bin", "w+");
    
        if(fp != NULL)
        {
            fwrite(x, sizeof(short), 10 /*20/2*/, fp);
            rewind(fp);
            fread(result, sizeof(short), 10 /*20/2*/, fp);
        }
        else
            return 1;
    
        printf("Result\n");
        for (i = 0; i < 10; i++)
            printf("%d = %d\n", i, (int)result[i]);
    
        fclose(fp);
        return 0;
    }
    

    With output:

    Result
    0 = 1
    1 = 2
    2 = 3
    3 = 4
    4 = 5
    5 = 6
    6 = 5000
    7 = 6
    8 = -10
    9 = 11
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My project has both client and server components in the same solution file. I
Project A contains a WPF Window (Data Entry Form) with a stack panel of
Project Euler has a paging file problem (though it's disguised in other words). I
Project: ASP.NET 3.5 with C# I have this much :- A table which used
My project is currently using a svn repository which gains several hundred new revisions
The project is ASP.NET 2.0, I have never been able to reproduce this myself,
The Project's Web section (under project properties in VS2008) has a list of debuggers:
Project: Take Wikipedia's list of Roman consuls, put the data in a CSV so
Project Euler 126 says: If we then add a second layer to this solid
Project file here if you want to download: http://files.me.com/knyck2/918odc So I am working on

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.