# Detecting empty file situation.

This is a discussion on Detecting empty file situation. within the C Programming forums, part of the General Programming Boards category; Hello: I'm in a small bind. In my program I'm opening a file that may be empty. I can write ...

1. ## Detecting empty file situation.

Hello:

I'm in a small bind. In my program I'm opening a file that may be empty. I can write the code around my problem of detecting whether or not a file is empty by setting my arrays to none zero values but I'd rather use a command if there is one. I checked through the tutorials and didn't find one. I know NULL is used for failing to open a file but is there a operator for a file that has no data in it? Any input will be appreciated. Thanks.

Icy

2. Well, a blank file should have only the EOF character. So you could use feof() to check if fp reaches end of file. Or fgetc() and check for EOF.

I don't know any ready function that does that. But it is like 1 line of code

3. Try something like this:
Code:
int main (void)
{
FILE *fp;
char *path = "path\\to\\file";
long size;

fp = fopen(path, "r");

if (fp)
{
fseek (fp, 0, SEEK_END);
size = ftell(fp);
printf("Size of the file in bytes: &#37;lu\n", size);
fclose(fp);
}

return 0;
}
If size is ever 0, you know the file is empty.

4. Originally Posted by C_ntua
Well, a blank file should have only the EOF character. So you could use feof() to check if fp reaches end of file. Or fgetc() and check for EOF.

I don't know any ready function that does that. But it is like 1 line of code
I've only used feof() in the form of a !feof(), I just tried it with the following code, and it didn't execute the code even though the file was empty. Any ideas?

Code:
    if(feof(IF))
{
printf("Data file is emtpy, exiting the program");
exit(0);
}

5. Originally Posted by xIcyx
I've only used feof() in the form of a !feof(), I just tried it with the following code, and it didn't execute the code even though the file was empty. Any ideas?

Code:
    if(feof(IF))
{
printf("Data file is emtpy, exiting the program");
exit(0);
}
If you want to use the feof method, try this:
Code:
int main (void)
{
FILE *fp;
char *path = "path\\to\\file";
long size = 0;

fp = fopen(path, "r");

if (fp)
{
while (!feof(fp))
{
fgetc(fp);
size++;
}
printf("Size of the file in bytes: &#37;lu\n", size);
fclose(fp);
}

return 0;
}
Keep in mind though that this way, an eof char will be returned, so it will say it contains one byte.

By the way, on http://www.cplusplus.com, they have many examples of functions and your case is one of them.

6. Thank you for the input carrotcake.

This is what I'm leaning twoards, I was writing my first reply while you posted so did not see it first.

Code:
    if(IF)
{
fseek (IF, 0, SEEK_END);
lSize = ftell(IF);
if(lSize == 0)
{
fclose(IF);
printf("\nInput file is empty, exting the program\n");
system("PAUSE");
exit(0);
}
}
Edit: I fixed my problem, no clue why it didn't work before. Thanks for the help guys, and the referral to a new site for resources on C.

7. Are you saying that your function is pausing, and then terminating? Or are you trying to say that your function isn't pausing?

8. Originally Posted by xIcyx
I've only used feof() in the form of a !feof(), I just tried it with the following code, and it didn't execute the code even though the file was empty. Any ideas?

Code:

if(feof(IF))
{
printf("Data file is emtpy, exiting the program");
exit(0);
}
Yeah. You would need to execute first an fgetc() then the above code.

9. Originally Posted by carrotcake1029
Are you saying that your function is pausing, and then terminating? Or are you trying to say that your function isn't pausing?
I fixed my problem, I honestly think it was working and because of all the password verification stuff I have getting into the program I took it as executing, just a stressed out mistake.

10. If handling EOF is an issue, you're probably doing it wrong.

Consider
Code:
int ch;
while ( (ch=fgetc(fp)) != EOF ) {
// do stuff here
}
or
Code:
char buff[BUFSIZ];
while ( fgets( buff, sizeof buff, fp ) != NULL ) {
// do stuff here
}
If the file is empty to begin with, the loops will exit without doing any "stuff"