Boost C++ Libraries: Ticket #11341: dimacs_basic_reader reads corrupted data https://svn.boost.org/trac10/ticket/11341 <p> As analyzed for <a class="ext-link" href="http://stackoverflow.com/questions/30415388/how-to-read-dimacs-vertex-coloring-graphs-in-c/30446685#30446685"><span class="icon">​</span>http://stackoverflow.com/questions/30415388/how-to-read-dimacs-vertex-coloring-graphs-in-c/30446685#30446685</a> </p> <p> The parsing of edge lines is fundamentally broken. </p> <p> A simple file as </p> <pre class="wiki">p edge 2 1 e 1 2 </pre><p> will fail to read correct data (without raising an error). Instead, indeterminate values are read for the edge, and the results are undefined (at the very least depend on the graph model). </p> <p> Related, I don't think the expression <code>(char*) buf.c_str()</code> is legal. I'd suggest at least replacing that by <code>&amp;buf[0]</code>. </p> <p> Further I'd really suggest replacing the parser. For the subset that the question was about, I've suggested two implementations in the linked answer on SO: </p> <pre class="wiki">#include &lt;string&gt; #include &lt;istream&gt; #include &lt;sstream&gt; template &lt;typename Graph&gt; bool read_coloring_problem(std::istream&amp; dimacs, Graph&amp; g) { size_t vertices = 0, edges = 0; std::string line; while (getline(dimacs, line)) { std::istringstream iss(line); char ch; if (iss &gt;&gt; ch) { size_t from, to; std::string format; switch(ch) { case 'c': break; case 'p': if (vertices||edges) return false; if (iss &gt;&gt; format &gt;&gt; vertices &gt;&gt; edges) { if ("edge" != format) return false; } break; case 'e': if (edges-- &amp;&amp; (iss &gt;&gt; from &gt;&gt; to) &amp;&amp; (add_edge(from-1, to-1, g).second)) break; default: return false; } } } return !(edges || !dimacs.eof()); } </pre><p> Or with Boost Spirit: </p> <pre class="wiki">#include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;boost/spirit/include/phoenix.hpp&gt; #include &lt;boost/spirit/include/qi_match.hpp&gt; template &lt;typename Graph&gt; bool read_coloring_problem(std::istream&amp; dimacs, Graph&amp; g) { auto add_edge_ = [&amp;g](size_t from, size_t to) { add_edge(from, to, g); }; size_t vertices = 0, edges = 0; using namespace boost::spirit::qi; namespace px = boost::phoenix; uint_parser&lt;size_t&gt; num_; auto eoil = eol | eoi; auto comment = boost::proto::deep_copy(lexeme["c " &gt;&gt; *(char_ - eol) &gt;&gt; eoil] | eol); auto vertices_ = px::ref(vertices); auto edges_ = px::ref(edges); dimacs &gt;&gt; std::noskipws &gt;&gt; phrase_match( *comment &gt;&gt; ("p" &gt;&gt; lit("edge") &gt;&gt; num_ [vertices_ = _1] &gt;&gt; num_ [edges_ = _1] &gt;&gt; eoil) &gt;&gt; repeat(edges_) [ *comment &gt;&gt; ("e" &gt;&gt; num_ &gt;&gt; num_ &gt;&gt; eoil) [ px::bind(add_edge_, _1-1, _2-1) ] ] &gt;&gt; *comment &gt;&gt; eoi , blank); return dimacs; } </pre><p> I realize there are missing features (<code>weight</code>) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11341 Trac 1.4.3 bugs@… Mon, 25 May 2015 23:25:25 GMT version, component changed; keywords, owner set https://svn.boost.org/trac10/ticket/11341#comment:1 https://svn.boost.org/trac10/ticket/11341#comment:1 <ul> <li><strong>keywords</strong> dimacs added </li> <li><strong>owner</strong> set to <span class="trac-author">Jeremiah Willcock</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.57.0</span> → <span class="trac-field-new">Boost 1.58.0</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">graph</span> </li> </ul> <p> (Oops. That went in prematurely. Site is really slow to handle) </p> <p> I realize there are missing features (weights?), and perhaps enabling the iterator-range construction is faster (?). The non-spirit approach might works better for header-only application (due to compile times). </p> <p> Let me know if you want help revisiting the parsing code here. </p> Ticket