Reputation: 9733
In C, how do I know from FILE* whether that file is empty or not?
Upvotes: 1
Views: 7169
Reputation: 16406
If you're coding for a POSIX system:
struct stat sb;
if( fstat(fileno(file), &sb) ) { /* error */ }
if( sb.st_size == 0 ) { /* file is empty */ }
Upvotes: 1
Reputation: 15289
Like this:
bool isEmpty(FILE *file)
{
long savedOffset = ftell(file);
fseek(file, 0, SEEK_END);
if (ftell(file) == 0)
{
return true;
}
fseek(file, savedOffset, SEEK_SET);
return false;
}
Upvotes: 5