fstream - C++ ifstream getline not working -
i'm trying read values config file in c++ , getline
doesn't seem working.
the relevant portion of code looks this:
#include <iostream> #include <fstream> #include <unistd.h> using namespace std; // ... ifstream config( configpath ); string line; message( diagverbose, "config: %s\n", configpath.c_str()); bool exists = ( access( configpath.c_str(), f_ok && r_ok ) != -1 ); message( diagverbose, "exists?: %s\n", exists ? "true" : "false"); // causing fail bit set //config.open( configpath ); if (config.is_open()) { message( diagverbose, "config open%s\n", config.good() ? "good" : "bad" ); while ( getline( config, line ) ) { message( diagverbose, "line: %s", line.c_str()); extension_t extension = parseconfig( line ); extensions[ extension.name ] = extension.type; } config.close(); } else { fatalerror( "could not open file %s\n", configpath.c_str()); }
the message function wrapper printf , prints following:
config: ./tool.conf exists?: true config open: 74181 segmentation fault: 11
but in while loop skipped. file i'm reading have data in it.
why getline
not getting line though file exists , readable?
update
i changed code above reflect changes suggested @rici however, i'm facing segmentation fault @ same line before in while
loop called.
you're opening config
twice (once in constructor, , again using explicit open
). second open
error, causes failbit set (even though ifstream still open.) on, i/o operations ifstream
fail.
Comments
Post a Comment