How to find if a file is empty

heidik's Avatar, Join Date: Oct 2010
Contributor
Could anyone please show me a working example of how to find if a file exists but is empty or not?

Thanks
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
heidik's Avatar, Join Date: Oct 2010
Contributor
I am working on Linux.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
heidik's Avatar, Join Date: Oct 2010
Contributor
oh OK. Let me try this.
heidik's Avatar, Join Date: Oct 2010
Contributor
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?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Yes we can have multiple methods to see if the file is empty or not.