?
Solved

Opening empty files...

Posted on 2003-03-31
5
Medium Priority
?
194 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:downatone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 60 total points
ID: 8241549
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
 

Author Comment

by:downatone
ID: 8241733
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
 

Author Comment

by:downatone
ID: 8242092
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
 
LVL 6

Expert Comment

by:GaryFx
ID: 8242581
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
 

Expert Comment

by:marcdm
ID: 8496571
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question