ifstream 2



1
2
3
4
5
6
7
 long begin , end;
ifstream file("READ.txt");
begin=file.tellg();
file.seekg(0,ios::end);
end=file.tellg();
file.close();
cout<<"SIZE IS " <<(end-begin)<<"bytes"<<endl;

Is tellg() begin of the file which should be equal to zero line 3 ?
Is seekg() end of the file which should be equal to total bytes in line 4 ?
In line 5 why do we need to repeat line3 with end variable ?
In line 7 i don't understand what is (end - begin ) what does this syntax mean?
Is tellg() begin of the file which should be equal to zero line 3 ?

Assuming that you just opened the file and haven't done any reading or writing, tellg() will be 0.

Is seekg() end of the file which should be equal to total bytes in line 4 ?

the seekg() call will move the file pointer to the position just after the last byte of the file. Technically, this is a position and not a file size, but since we started counting at zero they are the same value.

In line 5 why do we need to repeat line3 with end variable ?

because seekg() moves the pointer, and tellg() tells you where the current pointer position is.

In line 7 i don't understand what is (end - begin ) what does this syntax mean?

You have two values which are the pointer position at the beginning and the pointer position at the end. You need to subtract begin from end to get the total number of bytes in the file.
Thank you :)
Topic archived. No new replies allowed.