Boost logo

Boost Users :

Subject: Re: [Boost-users] [BGL] Encountering "bad lexical cast" exception when reading a class from graphviz file
From: Nicholas Mario Wardhana (mario.wardhana_at_[hidden])
Date: 2012-07-23 10:34:49


On 22 July 2012 04:09, Nicholas Mario Wardhana <mario.wardhana_at_[hidden]> wrote:
>
> Oops, I made a mistake. I just input "1" in my previous e-mail, so I
> changed it to "1 2 3", and this is what I get:
>
> Does it reach EOF? yes
> Is it bad? no
> Does it fail? no
> Is it good? no
>
> So it reaches EOF, and it is not good.
>
> The updated code and the output are at the end of this e-mail. It
> seems like the graphviz and lexical_cast cases suffer from setting the
> failbit, whereas the istringstream case sets the eofbit. No flag is
> set in the std::cin case.
>
> Thanks!
>
> Best regards,
> Nicholas
>

I finally solved this problem. The correct, at least better, way to
save/read the data is like mentioned in

http://stackoverflow.com/questions/10382884/c-using-classes-with-boostlexical-cast
,

which in my case becomes

//====================================

inline friend std::ostream& operator <<
    ( std::ostream& o, const Vec3& v )
{
    o << v.x << " " << v.y << " " << v.z;
    return o;
}

inline friend std::istream& operator >>
    ( std::istream& i, Vec3& v )
{
    i >> v.x;
    if((i.flags() & std::ios_base::skipws) == 0)
    {
        char whitespace;
        i >> whitespace;
    }
    i >> v.y;
    if((i.flags() & std::ios_base::skipws) == 0)
    {
        char whitespace;
        i >> whitespace;
    }
    i >> v.z;
    return i;
}

//====================================

This has also apparently been a topic of discussion for quite some time.

http://lists.boost.org/Archives/boost/2005/01/79275.php

Thank you again!

Best regards,
Nicholas


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net