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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:31:56+00:00 2026-05-16T11:31:56+00:00

While doing filing im stuck here.The condition of the while loop is not working.The

  • 0

While doing filing im stuck here.The condition of the while loop is not working.The compiler says cannot convert int to FILE*.

while(pFile!=EOF);

Should i typecase the pFile to int?I tried that but it did not worked.Thanks in advance.
The complete code is:

int main()
{
    char ch;
    char name[20];
    FILE *pFile;
    int score;
    pFile=fopen("database.txt","r");
    if(pFile!=NULL)
    {

         while(pFile!=EOF);
        {
        fscanf(pFile,"%c",ch);
        }
    }
    else
    printf("Cant open the file.......");
        fclose(pFile);
        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-05-16T11:31:56+00:00Added an answer on May 16, 2026 at 11:31 am

    First, you do not want to use while (!feof(pFile)) — ever! Doing so will almost inevitably lead to an error where the last data you read from the file appears to be read twice. It’s possible to make it work correctly, but only by adding another check in the middle of the loop to exit when EOF is reached — in which case, the loop condition itself will never be used (i.e., the other check is the one that will actually do the job of exiting the loop).

    What you normally do want to do is check for EOF as you read the data. Different functions indicate EOF in different ways. fgets signals failure (including EOF) by returning NULL. Most others (getc, fgetc, etc.) do return EOF, so you typically end up with something like this:

    int ch;   // Note, this should be int, NOT char
    
    while (EOF != (ch=getc(pFile)))
        process(ch);
    

    or:

    char buffer[MAX_LINE_SIZE];
    
    while (fgets(buffer, sizeof(buffer), pFile))
        process(buffer);
    

    With scanf, checking for success is a little more complex — it returns the number of successful conversions, so you want to make sure that matches what you expected. For example:

    while (1 == fscanf(fPfile, "%d", &input_number))
       process(input_number);
    

    In this case I’ve used 1 because I specified 1 conversion in the format string. It’s also possible, however, for conversion to fail for reasons other than EOF, so if this failes, you’ll frequently want to check feof(pFile). If it returns false, do something like reading the remainder of the line, showing it to the user in a warning message, and then continuing to read the rest of the file.

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

Sidebar

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.