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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:31:05+00:00 2026-06-09T15:31:05+00:00

I have a function that reads integers with certain format from a file. It

  • 0

I have a function that reads integers with certain format from a file.
It works fine as desired, but whenever I tried to close the file with fclose(),
fclose() always returns EOF.

Any suggestions why? I am a student and still learning.
I have put the code below. Please let me know if you need the “processing” code. Thanks 🙂

// Open the file
FILE *myFile = fopen(fileName, "r");
if(myFile == NULL){
    //Handle error
    fprintf(stderr, "Error opening file for read \n");
    exit(1);
}

while(myFile != EOF)
{
     // read and process the file
     // this part works.
}

// always returns EOF here. WHY?
if (fclose(myFile) == EOF) {
    // Handle the error!
    fprintf(stderr, "Error closing input file.\n");
    exit(1);
}
printf("Done reading the file.");

EDIT:
Thank you for all the replies. Sorry I cannot post the code as this is part of my homework. I was trying to get some help, I am not asking you guys to make the code for me. Posting code is illegal according to my Prof (since other students can see and probably copy, that’s what he told me). I can only post the code after Sunday. For now, I will try to modify my code and avoid using fscanf. Thanks and my apology.

  • 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-09T15:31:06+00:00Added an answer on June 9, 2026 at 3:31 pm

    This:

    while(myFile != EOF)
    

    is actually illegal (a constraint violation). Any conforming C compiler is required to issue a diagnostic; gcc, by default, merely issues a warning (which does qualify as a “diagnostic”).

    If gcc gave you a warning, you should pay attention to it; gcc often issues warnings for things that IMHO should be treated as fatal errors. And if it didn’t give you a warning, you’re probably invoking it with options that disable warnings (which is odd, because it does produce that warning by default). A good set of options to use is -Wall -Wextra -std=c99 -pedantic (or adjust the -std=... option to enforce a different version of the standard if you like).

    myFile is of pointer type, specifically FILE*. EOF is of type int, and typically expands to (-1). You cannot legally compare a pointer value to an integer value (except for the special case of a null pointer constant, but that doesn’t apply here.)

    Assuming the program isn’t rejected, I’d expect that to result in an infinite loop, since myFile would almost certainly never be equal to EOF.

    You could change it to

    while (!feof(myFile))
    

    but that can cause other problems. The correct way to detect end-of-file while reading from a file is to use the value returned by whatever function you’re using read the data. Consult the documentation for the function you’re using to see what it returns when it encounters end-of-file or an error condition. The feof() function is useful for determining, after you’ve finished reading, whether you encountered end-of-file or an error condition.

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

Sidebar

Related Questions

I have a function that reads a text file containing two lines, it reads
I have loads of c++ classes that reads data from a file stream. The
I have an example function below that reads in a date as a string
I have a function, that reads a registry key, get a path for a
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have the following Javascript code that works perfectly: $(document).ready(function() { $(#Select1).setDefault(); $(#Select2).setDefault(); $(#Select3).setDefault();
Say, I have a file that contains 100000 integers. Each integer is on a
The problem I have is reading in multiple lines of integers from a file
I have the problem that I must read Integers from a database. The varibles
I have a section of code in my $(document).ready(function() {} that reads: getInitial(len); Where

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.