Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > check filesize before reading it

Reply
Thread Tools

check filesize before reading it

 
 
Magix
Guest
Posts: n/a
 
      07-05-2004
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.


 
Reply With Quote
 
 
 
 
Alex Fraser
Guest
Posts: n/a
 
      07-05-2004
"Magix" <(E-Mail Removed)> wrote in message
news:40e92992$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Harti Brandt
Guest
Posts: n/a
 
      07-05-2004
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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-05-2004
In <40e92992$(E-Mail Removed)> "Magix" <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-06-2004
In <(E-Mail Removed)> Harti Brandt <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How check filesize using FileUpload control? VB Programmer ASP .Net 11 09-01-2012 09:05 AM
Validate Filesize before upload Tom Cole Javascript 2 09-08-2009 01:31 PM
Response.writefile (filename) ?? problem with filesize =?Utf-8?B?ZG90bmV0dGVzdGVy?= ASP .Net 2 08-16-2005 03:04 PM
Problem retrieving filesize David Colliver ASP .Net 0 05-17-2005 12:43 PM
Format filesize in kB or MB Arjen ASP .Net 2 08-09-2003 03:56 AM



Advertisments