std::streampos pos_ = fstrm.tellg ();
ostrm << pos_; // this works
...
istrm >> pos_; // compiler error (no operator >> defined for std::streampos)
int ipos;
istrm >> ipos;
pos_ = static_cast<std::streampos> (ipos); // ???? is this allowed ???
This works for g++3.1.1 and MSVC6.0, but is this portable ??
Thanks in advance,
Chris
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Christian Sulzer schrieb:
> Hi,
> Is there a way to convert std::streampos to an integral type
> (int or long) and back to streampos in a portable way ?
> Here's my problem:
>
> std::streampos pos_ = fstrm.tellg ();
> ostrm << pos_; // this works
> ...
> istrm >> pos_; // compiler error (no operator >> defined for std::streampos)
>
> int ipos;
> istrm >> ipos;
> pos_ = static_cast<std::streampos> (ipos); // ???? is this allowed ???
This should work. Actually you could also write
pos_ = std::streampos(ipos);
First note, that std::streampos actually is a typedef:
typedef fpos<char_traits<char>::state_type> streampos;
of the std::fpos template class. This class has been designed for positional
semantics in files. Because files may have lengths (and thus positions inside)
which exceed the range of an int, it is implementation defined, whether a cast
from streampos -> int (or long) will work as expected. The requirements on the
fpos-template class (27.4.3.2) just say, that an fpos can be initialized by an
int or std::streamoff and that a std::streamoff can be initialized by an fpos.
std::streamoff itself is an implemenation defined type, so this may not really
help you on your way from fpos -> int/long.
Yours
Daniel Spangenberg