How to find if a file is empty

Discussion in 'C' started by heidik, Oct 28, 2010.

  1. heidik

    heidik New Member

    Joined:
    Oct 23, 2010
    Messages:
    69
    Likes Received:
    0
    Trophy Points:
    0
    Could anyone please show me a working example of how to find if a file exists but is empty or not?

    Thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What is your OS? In Windows read the file size and check if it is 0 or not but for Linux there will be no zero file size for even an empty file as far as I remember.
     
  3. heidik

    heidik New Member

    Joined:
    Oct 23, 2010
    Messages:
    69
    Likes Received:
    0
    Trophy Points:
    0
    I am working on Linux.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    So then check the size of the empty file in your Linux and I guess it will be 4bytes or something but not sure.

    If you read the file and get its size to be equal to your empty file size you assume it to be an empty file.
     
  5. heidik

    heidik New Member

    Joined:
    Oct 23, 2010
    Messages:
    69
    Likes Received:
    0
    Trophy Points:
    0
    oh OK. Let me try this.
     
  6. heidik

    heidik New Member

    Joined:
    Oct 23, 2010
    Messages:
    69
    Likes Received:
    0
    Trophy Points:
    0
    The getline(fileName,line) also reads the last line that is an empty line. Is there any way not to read that last empty line?
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Yes we can have multiple methods to see if the file is empty or not.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice