Thursday, July 30, 2009

How can I test a file to see if it is empty using an 'if' statement?

Using C++ how do you test an input file to see if it is empty or not, by using an 'if' statement.

How can I test a file to see if it is empty using an 'if' statement?
There is a file length function to see if a file is empty.





In 'C',





uses stdio.h functions


fseek(fp, 0, SEEK_END);


if (ftell(fp) == 0)


// empty file


fseek(fp, 0, SEEK_SET);





uses io.h functions


fd = open("FILE", O_RDONLY | O_BINARY);


if (filelength(fd) == 0)


// empty file


close(fd);





In 'C++'


istream ifs("FILE");


ifs.seekg(0, ios::end);


if (ifs.tellg() == 0)


// empty file


ifs.close();
Reply:I dont know c++ but look for a LOF() function , length of file .


No comments:

Post a Comment