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-21 16:09:06


On 22 July 2012 03:24, Nicholas Mario Wardhana <mario.wardhana_at_[hidden]> wrote:
>
> Thanks for your reply, Jeremiah!
>
> I tried lexical_cast<Point>("1 2 3") and it fails with the same exception too.
>
> As for the istringstream part, my code is
>
> std::istringstream iStringStream("1");
> Point p2;
> std::istream& returnValue = iStringStream >> p2;
> std::cout << "Does it reach EOF? " << (returnValue.eof()? "yes" :
> "no") << std::endl;
> std::cout << "Is it bad? " << (returnValue.bad()? "yes" : "no") << std::endl;
> std::cout << "Does it fail? " << (returnValue.fail()? "yes" : "no") <<
> std::endl;
> std::cout << "Is it good? " << (returnValue.good()? "yes" : "no") << std::endl;
>
> This is what it prints.
>
> Does it reach EOF? yes
> Is it bad? no
> Does it fail? yes
> Is it good? no
>
> So, I suppose the stream reaches EOF, and it is not bad, but it fails,
> and therefore it is not good. I wonder that these mean? I think I'll
> search more about that...
>
> Thanks again.
>
> Best regards,
> Nicholas

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

//------------------------code------------------------

#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/graph/graphviz.hpp>

#include <iostream>
#include <fstream>
#include <string>

struct Point
{
    float x;
    float y;
    float z;

    inline friend std::istream& operator >>
        ( std::istream& i, Point& p )
    {
        bool iToBool1 = i;
        i >> p.x >> p.y >> p.z;
        bool iToBool2 = i;
        std::cout << "Does it reach EOF? " << (i.eof()? "yes" : "no")
<< std::endl;
        std::cout << "Is it bad? " << (i.bad()? "yes" : "no") << std::endl;
        std::cout << "Does it fail? " << (i.fail()? "yes" : "no") << std::endl;
        std::cout << "Is it good? " << (i.good()? "yes" : "no") << std::endl;
        return i;
    }

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

struct Vertex
{
    int ID;
    Point p;
};

struct Edge
{
    int ID;
};

typedef boost::adjacency_list<
            boost::listS,
            boost::vecS,
            boost::undirectedS,
            Vertex, // bundled data
            Edge, // bundled data
            boost::no_property, // not needed
            boost::listS> Graph;

int main()
{
    // Part 1: input via std::cin
    std::cout << "Part 1: input via std::cin" << std::endl;
    Point p;
    std::cin >> p;
    Graph graph;

    // Part 2: input from graphviz
    std::cout << std::endl << "Part 2: input from graphviz" << std::endl;
    boost::dynamic_properties dynamicProperties;
    dynamicProperties.property("NodeID", boost::get(&Vertex::ID, graph));
    dynamicProperties.property("Point", boost::get(&Vertex::p, graph));
    dynamicProperties.property("EdgeID", boost::get(&Edge::ID, graph));

    std::ifstream file("graph.txt");
    try
    {
        boost::read_graphviz(file, graph, dynamicProperties, "NodeID");
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }

    // Part 3: test lexical_cast
    std::cout << std::endl << "Part 3: test lexical_cast" << std::endl;
    try
    {
        boost::lexical_cast<Point>("1 2 3");
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }

    // Part 4: istringstream
    std::cout << std::endl << "Part 4: istringstream" << std::endl;
    std::istringstream iStringStream("1 2 3");
    Point p2;
    iStringStream >> p2;

    return 0;
}

//------------------------output------------------------

Part 1: input via std::cin
1 2 3
Does it reach EOF? no
Is it bad? no
Does it fail? no
Is it good? yes

Part 2: input from graphviz
Does it reach EOF? no
Is it bad? no
Does it fail? yes
Is it good? no
bad lexical cast: source type value could not be interpreted as target

Part 3: test lexical_cast
Does it reach EOF? no
Is it bad? no
Does it fail? yes
Is it good? no
bad lexical cast: source type value could not be interpreted as target

Part 4: istringstream
Does it reach EOF? yes
Is it bad? no
Does it fail? no
Is it good? no


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