tags:

views:

80

answers:

6

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  A: 

here is correct way:

c = getc(pFile);
while (c != EOF) {
    /* Echo the file to stdout */
    putchar(c);
    c = getc(pFile);
}
if (feof(pFile))
  puts("End of file was reached.");
else if (ferror(pFile))
  puts("There was an error reading from the stream.");
else
  /*NOTREACHED*/
  puts("getc() failed in a non-conforming way.");

fclose(pFile);
Andrey
feof? :O ...Now it says Constant out or range.
fahad
This should be simply `while(!feof(pFile));`.
Oli Charlesworth
@fahad put more code
Andrey
-1: This is a classic error. If you do while( !feof( pfile )) { code }, then the final value of the file will be read, the next check of feof will return false, the code will again execute (reading eof and doing something with the bogus value), and on the next check of the condition the loop will terminate. You probably *never* want to write "while( !feof ... ) "
William Pursell
-1 -- `while (!feof(file))` is essentially always wrong. Following it with a semicolon is even worse -- it means nothing can ever change the condition, so if it executes at all, it'll be an infinite loop.
Jerry Coffin
@Jerry Coffin thank your for lesson. i just corrected what author wrote, since he posted other part of code later
Andrey
Yup -- good correction. Downvote removed...
Jerry Coffin
+1  A: 

Assuming pFile is your file handle, this doesn't change as you read from the file. EOF is returned by e.g. fgetc(). See e.g. http://www.drpaulcarter.com/cs/common-c-errors.php#4.2 for common ways to solve this.

Oli Charlesworth
getchar() works on stdin only. You are thinking of getc()/fgetc().
JeremyP
A: 

I'm guessing you want to keep looping while you haven't hit end-of-file. In that case, you are looking for this:

while (!feof(pFile))
{
    ...
}

That said, this is still not quite correct. feof will only return true once it tries to read beyond the end of the file. This means feof can return false and yet there is no more data to read. You should really try your operation and only check for end of file if it fails:

char buffer[SIZE];
while (fgets(buffer, sizeof(buffer), pFile))
{
    ...
}
if (!feof(pFile))
{
    // fgets failed for some reason *other* then end-of-file
}
R Samuel Klatchko
Be careful! If your loop content is e.g. `fgets(line, sizeof(line), fp); fputs(line, stdout);`, this won't work correctly.
Oli Charlesworth
+2  A: 

It depends what pFile and EOF are defined as, but I will asssume that pFile is a *FILE, and EOF is from stdio.h. Then I guess you should do something like:

#include <stdlib.h>
#include <stdio.h>

#define FILENAME "file.txt"

int main(void) {
    FILE *pFile;
    int ch;

    pFile = fopen(FILENAME,"r");

    if (pFile) {
        while ((ch = getc(pFile)) != EOF) {
            printf("Read one character: %c\n", ch);
        }
        close(pFile);
        return EXIT_SUCCESS;
    } else {
        printf("Unable to open file: '%s'\n", FILENAME);
        return EXIT_FAILURE;
    }
}

which yields

$ echo "abc" > file.txt
$ /tmp/fileread 
Read one character: a
Read one character: b
Read one character: c
Read one character: 
# last character being a linefeed
nicomen
+5  A: 

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.

Jerry Coffin
+1  A: 

pFile is a pointer to a file. EOF is usually defined as -1, a signed integer.

What you should do is fopen, make sure pFile != NULL, then call some function on the file handle until that function returns EOF. A pointer will (or rather, should) never be EOF. But a function acting on that pointer may return EOF.

Kizaru