Standard Library types

A

Alex J. Dam

Hi,

I'm reposting this since I posted it as an answer and the topic changed a
little (guess no one bothered looking at it).

If I need to store the location of some data within a binary file, in that
same binary file, should I store the pos_type value returned by
ostream::teelp()? (That would be strange. I would be storing extra
information, since I need only the position).

I'm confused as to what types should be used when calling Standard Library
functions, especially when mixing variables of different types, e.g., if I
divide an off_type by a size_t, what do I get?

Thanks.
 
V

Victor Bazarov

Alex J. Dam said:
If I need to store the location of some data within a binary file, in that
same binary file, should I store the pos_type value returned by
ostream::teelp()? (That would be strange. I would be storing extra
information, since I need only the position).

I'm confused as to what types should be used when calling Standard Library
functions, especially when mixing variables of different types, e.g., if I
divide an off_type by a size_t, what do I get?

I would say, you get off_type. If it's an arithmetic type, a value of
that type retains its type no matter how you slice it or dice it.

My solution was always simpler: since binary files are platform-specific
(in most cases, anyway), then store the position as a long or unsigned
long (or any other type that can accommodate the maximum size of the
file). Once you retrieve it, convert it to the necessary off_type or
pos_type or whatever.

V
 
J

Jonathan Turkanis

Alex J. Dam said:
Hi,

I'm reposting this since I posted it as an answer and the topic changed a
little (guess no one bothered looking at it).

If I need to store the location of some data within a binary file, in that
same binary file, should I store the pos_type value returned by
ostream::teelp()? (That would be strange. I would be storing extra
information, since I need only the position).

I'm confused as to what types should be used when calling Standard Library
functions, especially when mixing variables of different types, e.g., if I
divide an off_type by a size_t, what do I get?

Be aware that a stream's pos_type is not an intergral type, although
it can be converted to one. In general, this conversion could loose
information, although this may be unlikely with a stream in binary
mode.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top