check filesize before reading it

Discussion in 'C Programming' started by Magix, Jul 5, 2004.

  1. Magix

    Magix Guest

    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.
     
    Magix, Jul 5, 2004
    #1
    1. Advertising

  2. Magix

    Alex Fraser Guest

    "Magix" <> wrote in message
    news:40e92992$...
    > 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
     
    Alex Fraser, Jul 5, 2004
    #2
    1. Advertising

  3. Magix

    Harti Brandt Guest

    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
     
    Harti Brandt, Jul 5, 2004
    #3
  4. Magix

    Dan Pop Guest

    In <40e92992$> "Magix" <> 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, Jul 5, 2004
    #4
  5. Magix

    Dan Pop Guest

    In <> Harti Brandt <> 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, Jul 6, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arjen

    Format filesize in kB or MB

    Arjen, Aug 8, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,141
    Jerry III
    Aug 9, 2003
  2. David Colliver

    Problem retrieving filesize

    David Colliver, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    378
    David Colliver
    May 17, 2005
  3. =?Utf-8?B?ZG90bmV0dGVzdGVy?=

    Response.writefile (filename) ?? problem with filesize

    =?Utf-8?B?ZG90bmV0dGVzdGVy?=, Aug 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    504
    Buddy Ackerman
    Aug 16, 2005
  4. VB Programmer

    How check filesize using FileUpload control?

    VB Programmer, Jan 18, 2006, in forum: ASP .Net
    Replies:
    11
    Views:
    20,407
  5. Tom Cole

    Validate Filesize before upload

    Tom Cole, Sep 8, 2009, in forum: Javascript
    Replies:
    2
    Views:
    107
    tcole6
    Sep 8, 2009
Loading...

Share This Page