I am interested in knowing the size of a file in C++ using the stream classes of stl.
In order to accomplish that, long ago, I opened the file and then I performed some such as:
file.seekp(0, ios_base::end); // another way is open file with ios::ate mode
unsigned long file_size = file.tellp();
According to the file type and the access mode, the same thing could be accomplished with seekg()
and tellg()
.
However, reading a little about the doc, especially the Josuttis book about the stl, I see that the return type of tellp()
is pos_type
and not unsigned long
or any similar type (size_t
, ...).
This way of knowing the file size works still, at least for binary files, but I am not conform because I am not sure about its safety and portability given the fact that pos_type
could not cast correctly to an unsigned long
.
So I have the following questions:
- Is this way for knowing the file size (the mine) still sure? should I change it?
- there are other ways?