Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   check filesize before reading it (http://www.velocityreviews.com/forums/t434406-check-filesize-before-reading-it.html)

Magix 07-05-2004 10:18 AM

check filesize before reading it
 
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.



Alex Fraser 07-05-2004 10:53 AM

Re: check filesize before reading it
 
"Magix" <magix@asia.com> wrote in message
news:40e92992$1_2@news.tm.net.my...
> 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



Harti Brandt 07-05-2004 11:18 AM

Re: check filesize before reading it
 
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

Dan Pop 07-05-2004 02:44 PM

Re: check filesize before reading it
 
In <40e92992$1_2@news.tm.net.my> "Magix" <magix@asia.com> writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de

Dan Pop 07-06-2004 01:57 PM

Re: check filesize before reading it
 
In <20040705131807.R90892@beagle.kn.op.dlr.de> Harti Brandt <brandt@dlr.de> writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


All times are GMT. The time now is 11:50 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.