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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:16:47+00:00 2026-05-26T08:16:47+00:00

Hello every one this is basically extension to my previous question. i have write

  • 0

Hello every one this is basically extension to my previous question. i have write the short int value in file using

short int x= 254;
FILE * f1 = fopen("infile" ,"w");
fwrite (&x , 1 , sizeof(short int ) , f1 );

it working fine but when i tried to retrieve value like this

short int y ;
fread(&y , 2, 1 ,f1);
printf("%d"  , y);

it gave me answer 8180 and next time 12276 so on… what should i do

actually I want to store short integers in my file and then retrieve them one by one am i doing it wrong kindly guide me

  • 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-26T08:16:48+00:00Added an answer on May 26, 2026 at 8:16 am

    If you want to read back from the file you just wrote to, you need to open it with “w+” options to allow you to read and write. You then need to seek back to the beginning of the file:

    // Open the file
    const char* fname = "infile";
    FILE * f1 = fopen("infile" ,"wb+");
    if( f1 != NULL )
    {
        printf("opened file '%s' for writing / reading\n", fname);
    
        // Write two bytes
        short int x = 254;
        size_t bytes_written = fwrite(&x, 1, sizeof(short int), f1 );
        printf("%hd bytes written\n"  , bytes_written);
    
        // Seek to the start of the file
        int seek_result = fseek(f1, 0, 0);
        if( seek_result == 0 )
        {
            printf("found beginning of file\n");
        }
        else
        {
            printf("failed to seek to beginning of file\n");
        }
    
        // Read the two bytes back in
        short int y = -9999;
        size_t bytes_read = fread(&y, 1, sizeof(short int), f1);
        printf("%hd bytes read: %hd\n", bytes_read, y);
    
        // We're done with the file so close it
        fclose(f1);
    }
    else
    {
        // Something went wrong and we failed to open the file
        printf("failed to open file '%s' for writing / reading\n", fname);
    }
    

    Alternatively, you could read / write as separate operations:

    // Open the file
    const char* fname = "infile";
    FILE * f1 = fopen("infile" ,"wb");
    if( f1 != NULL )
    {
        printf("opened file '%s' for writing\n", fname);
    
        // Write two bytes
        short int x = 254;
        size_t bytes_written = fwrite(&x, 1, sizeof(short int), f1 );
        printf("%hd bytes written\n"  , bytes_written);
    
        // We're done with the file so close it
        fclose(f1);
    }
    else
    {
        // Something went wrong and we failed to open the file
        printf("failed to open file '%s' for writing\n", fname);
    }
    
    // re-open the file
    FILE * f2 = fopen("infile" ,"rb");
    if( f2 != NULL )
    {
        printf("opened file '%s' for reading\n", fname);
    
        // Read the two bytes back in
        short int y = -9999;
        size_t bytes_read = fread(&y, 1, sizeof(short int), f2);
        printf("%hd bytes read: %hd\n", bytes_read, y);
    
        // We're done with the file so close it
        fclose(f2);
    }
    else
    {
        // Something went wrong and we failed to open the file
        printf("failed to open file '%s' for reading\n", fname);
    }
    

    Note that in your original code you weren’t checking the return value of fread to check whether you’d actually read anything from the file. If you’d done that you’d have seen it was returning 0 to indicate that it read 0 bytes. If you’re not reading anything from the file and you’re not initialising y then it’s likely to just be a random number.

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

Sidebar

Related Questions

hello every one i have table view with one cell,and i make on this
Hello every one here I have a problem. I have a file in which
Hello every one i have strange bug with dropdownlist on one of the pages!
hello every one I want to ask a question about forking here is the
Hello fellow developers, I am a little stumped on this one as I have
I have a Activity called main. If I call Toast.makeText(this, Hello World from main,
Hello every one. I'm faced with a weird problem. My application has a simple
I have read some tutorials for bezier curve such as this one http://www.codeproject.com/KB/recipes/BezirCurves.aspx .
hello every one i want to pass the php array from the javascript function
hello every one i want to refresh my listview after adding or removing any

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.