check filesize before reading it

M

Magix

Hi,

I would like to check if the data file is empty or not before reading it.
How should I achieve that ? fseek/ftell ?

....fopen process here(..)
if filesize is 0 // mean empty data
printf ("No data in the file ");
else
...do the reading...

Thanks.
 
A

Alex Fraser

Magix said:
I would like to check if the data file is empty or not before reading it.
How should I achieve that ? fseek/ftell ?

I wouldn't, specifically. An empty data file is just a special case of the
data file being "short"; it would probably be best to handle the more
general case. You can do this by checking the return value of whatever
functions you use to read from the file, and calling feof() to determine if
end-of-file was the cause.

Alex
 
H

Harti Brandt

On Mon, 5 Jul 2004, Magix wrote:

M>I would like to check if the data file is empty or not before reading it.
M>How should I achieve that ? fseek/ftell ?
M>
M>...fopen process here(..)
M>if filesize is 0 // mean empty data
M> printf ("No data in the file ");
M>else
M> ...do the reading...

man stat

on Posix systems.

harti
 
D

Dan Pop

In said:
I would like to check if the data file is empty or not before reading it.
How should I achieve that ? fseek/ftell ?

Why do you want to do that, when it is so much easier to try reading a
character/byte from the file and see if you have succeeded or not?
After that, you can push back the read character into the stream.
...fopen process here(..)
if filesize is 0 // mean empty data if ((c = getc(fp)) == EOF)
printf ("No data in the file ");
else
{
ungetc(c, fp);
...do the reading...

Dan
 
D

Dan Pop

In said:
On Mon, 5 Jul 2004, Magix wrote:

M>I would like to check if the data file is empty or not before reading it.
M>How should I achieve that ? fseek/ftell ?
M>
M>...fopen process here(..)
M>if filesize is 0 // mean empty data
M> printf ("No data in the file ");
M>else
M> ...do the reading...

man stat

on Posix systems.

If he wanted a solution for POSIX systems, what was preventing him from
asking in comp.unix.programmer?

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top