Checking the file

Hello,
I'm having difficulties trying to think of a way to check if a file is empty or not. I have an assignment which says something of the sorts " Check if the file is empty or not if empty display a message." Something like this. I'm just having a brain fart. I was thinking about just having a loop to keep bringing in a char variable and if that variable has something other then ' ' return true or whatever. I don't know. Possible ideas please.
Thanks much,
j
Check if file size is 0 bytes.
How do you do that that is there a function for that??
Thanks I wrote one in parrot fashion. I get a message that says that not all control paths return a value though when I run my function. Here she is ..

1
2
3
4
5
6
7
8
9
10
int BadFile(ifstream &fin)
{	
	int filelength;
	fin.seekg(0, ios::end);
	filelength = fin.tellg();
	if(filelength == 0)
		return -1;
	else if(filelength !=0)
		return  1;
}


So my question is fin.seekg(0, ios::end) what exactly does ios::end mean? I know that is is telling it to go to the end but could you use eof? I'm just wondering why you write this that little bit confuses me. Also do I have to return the pointer to the beginning will this cause problems after I have used this function? Can you see why it would say not all control paths return a value when it compiles? Thanks
tellg returns your current position. When you open the file, you're at position 0, but you don't know if there is a character there or not. By calling seekg, you're saying move to the end of the file, now tellg will give you how many characters are in the file.

You don't need to return the get pointer to the beginning if the file is empty. You still at the beginning. If the file is not empty, then yes, you need to move back to the beginning.

You're getting the control path message because of the structure of your if. The compiler is telling you there is no return statement if the if at line 8 is false. In reality, you don't need the second if.

1
2
3
4
	if(filelength == 0)
	    return -1;
	else 
                    return 1;

OK I rewrote my if and I'm still getting the warning.
Here is what I have now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Function to test for Empty file
int BadFile(ifstream &fin)
{	
	int filelength;
	//To determine file length 
	fin.seekg(0, ios::end);
	filelength = fin.tellg();
	// Selection of return value
	if(filelength == 0)
		return -1;
	else if(filelength != 0)
		return  1;
	//Return pointer to the begining of file
	fin.seekg(0, ios::beg);

}


So I think that the warning would go away because both if's return something. But I guess this is not the case.
Thanks.
In the previous response, how will line 14 ever get executed? I think it would execute in the case when the length is neither zero, nor is it non-zero.

seekg() is telling the computer to move the pointer a certain number of bytes along. The values ios::beg, ios::cur and ios::end are just values which the machine understands to mean, "from the beginning" etc.

eof is a different type of animal. It's a condition which occurs when you attempt to read some data, but there is nothing more to read. It isn't related to position as such, but simply to the success or otherwise of a read operation.

The problem with your logic (from the compiler's point of view) is that the final if does not have an else, so it considers there may be some condition where the if is not true. After all, if that were not the case, why would you bother using an if at all?

And the fact that you restored the file position in that scenario shows you agree with the compiler. :)

As for resetting the file position, a well-behaved function would first of all store the current position, and then restore that position when you are finished.

One more thing, since all you ever return is a yes/no result, this seems a good candidate for a boolean return type, not an integer.
1
2
3
4
5
6
7
8
9
10
bool BadFile(ifstream &fin)
{
    unsigned int pos = fin.tellg();        // save current position

    fin.seekg(0, ios::end);                // Set position zero bytes from the end
    unsigned int filelength = fin.tellg(); // get the length
    fin.seekg(pos, ios::beg);              // restore saved position

    return (filelength == 0);              // return true if file is empty
}
Last edited on
I don't see how you rewrote the if. It looks exactly the same.

You're getting the warning now because there is no return statement after the call to seekg, which by the way you will never reach.
Topic archived. No new replies allowed.