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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:30:41+00:00 2026-06-10T03:30:41+00:00

Possible Duplicate: “while( !feof( file ) )” is always wrong If I write an

  • 0

Possible Duplicate:
“while( !feof( file ) )” is always wrong

If I write an array to the output file and I close the file, then open the file again and read everything until end-of-file is reached, although the file contains only 4 number, the program will read and print 5 numbers, why?

Program output:

a[0] = 4
a[1] = 7
a[2] = 12
a[3] = 34
a[4] = 34

save.bin (with a hex editor)

04000000 07000000 0C000000 22000000

#include <stdio.h>
#include <stdlib.h>
#define path "save.bin"

int main(void)
{
  FILE *f=NULL;
  int a[]={4,7,12,34},i,n=4,k;
  f=fopen(path,"wb");
  if(f==NULL)
  {
    perror("Error");
    exit(1);
  }
  for(i=0;i<n;i++)  // or I could use fwrite(a,sizeof(int),n,f);
    fwrite(&a[i],sizeof(int),1,f);
  fclose(f);
  f=fopen(path,"rb");
  if(f==NULL)
  {
    perror("Error");
    exit(1);
  }
  i=0;
  while(!feof(f))
  {
    fread(&k,sizeof(int),1,f);
    printf("a[%d] = %d\n",i,k);
    i++;
  }
  printf("\n");
  fclose(f);
  return 0;
}
  • 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-10T03:30:43+00:00Added an answer on June 10, 2026 at 3:30 am

    feof(fp) becomes false (i.e. non-zero value) only if you tried to read past the end of file. That should explain why the loop is entered one more than what you expect.

    From the documentation:

      The function feof() tests the end-of-file indicator for the stream
      pointed to by stream, returning nonzero if it is set.  The end-of-
      file indicator can be cleared only by the function clearerr().
    

    Also read the post: Why is “while ( !feof (file) )” always wrong?

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

Sidebar

Related Questions

Possible Duplicate: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
Possible Duplicate: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
Possible Duplicate: Getting “Permission denied” page in the admin, while user has permission I've
Possible Duplicate: “On file dialog cancel” event in JavaScript Is there a listener for
Possible Duplicate: “Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given” error while
Possible Duplicate: C# newbie: what’s the difference between “bool” and “bool?” ? Hi, While
Possible Duplicate: “No newline at end of file” compiler warning i am a Linux
Possible Duplicate: jQuery Ajax always returns “undefined”? I'm trying to come up with a
Possible Duplicate: Why have “while(1);” in XmlHttpRequest response? If you take a peek at
Possible Duplicate: Why write <script type=“text/javascript”> when the mime type is set by the

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.