Boost logo

Boost :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2004-07-01 16:33:40

I'm posting this message from Gustav Lead from comp.lang.c++ while he gets
access to boost devel and/or user mailing lists.

Jeff Flinn

Hi all,

I have trouble reading back floating point data from a file.

It appears as if the tokens for positive and negative infinity (1.#INF
and -1.#INF) that my implementation writes get misinterpreted (to 1.0
and -1.0) when I try to read them back in.

    #include <fstream>
    #include <limits>

    void somefunction()

    double d_infinity = std::numeric_limits<double>::infinity();
    std::ofstream output;"file.txt");
    output << d_infinity;

    double d_test1 = 0;
    std::ifstream input;"file.txt");
    input >> d_test1;


The contents of "file.txt" is "1.#INF".
At the end of the function the value of d_test1 is 1.0 .
Trying to read in more data after that will fail, because the "#INF"
fragment remains in the stream.

Is this a problem of my STL implementation (I am using MSVC 7.1), or do
I have to make some change to my code to get this to work correctly?

Thanks in advance,

"Mike Wahler" <mkwahler_at_[hidden]> wrote in
> No, this is by design.

> If you want to store a special 'token' to indicate 'infinity', you'll
> need to specifically parse for it.
> Typically numeric input is handled more robustly by reading a 'line'
> as a string, and using conversion functions after the fact (e.g.
> 'strod()' or an istringstream extractor). To check for your
> 'infinity' token, just search the string for that first.
> -Mike

Thanks for your answer, Mike!

Unfortunatly this is not an option for me, because I am using a
serialization library (Boost::Serialization currently at and I have to rely on the STL stream library
operators to read back all of their repective data as they wrote it into
the stream and to properly restore the state of the object.

Perhaps I could alter the behavior of the "ostream& operator<<(ostream&,
double)" (and istream)?

I think overloading would be a redefintion and is out of the question
(or is it???) unless I make some kind of wrapper for doubles, but maybe
it is possible to do this with locales (or facets?) ?

Locales are really terra incognita for me, so I'd really like some
advice if this is a viable option at all, before I try to go down that

Any other idea for a solution would of course be just as welcome....


Boost list run by bdawes at, gregod at, cpdaniel at, john at