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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -