Hi everyone. I was wondering what is the best way to check if a text file is empty?

When I read from a text file:

file.Open(path, CFile::modeRead);

And then try to access it:

file.ReadString(line)

I get an error..

I have tried if 'file.end' but file.end returns true every time so I think I got the wrong method...

Recommended Answers

All 4 Replies

You are writing an MFC program so surly by now you have learned to read the MSDN docs for each of the MFC functions you use. You do know about that don't you ?

Read this and you will find the function you are looking for.

Hi there. I have looked through it and couldn't find one which checks if it's empty. I was thinking about using the GetLength function to see if it's 0, but doesn't all text files end with a '\0'? Or does it not contain anything at all if it's empty?

If the file is empty that means its length is 0. You can verify it when Windows Explorer. The GetLength() function is what you want.

Thank you again! I will try and think more like you in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.