• C

Opening empty files...

Hi there, assuming this should be a pretty simple question. Given it a few tries but to no avail! I'm trying to check if a file I'm opening is empty, but can't figure out how to do it. Any pointers?

FILE* ptr;
ptr = fopen("text.txt","r");

Sum her up:
How do I check if the text.txt file has nothing in it?

cheers
David
downatoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryFxCommented:
Try using stat or fstat (at least on UNIX):

#include <sys/types.h>
#include <sys/stat.h>
...

struct stat buf;
int rc;
...
rc = stat("text.txt", &buf);
if (rc == 0 ) {
  if (buf.st_size == 0) {
     // File is empty
  }
}

Gary
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
downatoneAuthor Commented:
Gary,
Thanks for the quick reply.
Works great, just one more little thing sorry :)
When I go to replace "text.txt" with an actual file pointer:

FILE* filePtr;
filePtr = fopen("text.txt","r");

...
rc = stat(filePtr, &buf);
...

I'm getting this compile error:
tmp.c:23: warning: passing arg 1 of `stat' from incompatible pointer type

Any further thoughts?
cheers
David
0
downatoneAuthor Commented:
No worries, found a way around that...still if you do have an answer I'm curious as too how you found a way around it??? cheers
0
GaryFxCommented:
Hmm.  I've never really thought about that.  You can use open instead of fopen to create the file, passing the resulting descriptor to fstat to get the information, and passing the same descriptor to fdopen to get a FILE*.  But I don't know of any way to get a file descriptor from a stream file (FILE*) pointer (which seems silly).  

You could in theory address the original problem by attempting to read one byte, and figuring out whether or not it returns.  But then you'd either have to maintain the byte so that it's available when you really want it, or else fseek back to the beginning, or else close and reopen.  All of these are legitimate workarounds but they're significantly more work than using stat or fstat.

So if I really needed to use FILE descriptors, I'd go with my open/fdopen strategy above.

Gary
0
marcdmCommented:
A possible solution could be (starting from your code) :

FILE* ptr;
ptr = fopen("text.txt","r");

long Fo;    // File Offset

if (fseek(ptr,0,SEEK_END) == 0)
{
    Fo = ftell(ptr);
    if (Fo == 0)
    {
        // File is Empty ...
    }
    else
    {
        // File is NOT Empty ...
    }
}
else
{
    // Error ... !
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.