[iostreams] Question about correct opened file
Hi, I've the following code: namespace io = boost::iostreams; bool foo::open( const std::string& file ) { io::filtering_istream is; is.push( my_line_filter() ); is.push( io::file_source( file ) ); // ??? how to check success do { std::string line; std::getline(is, line); std::cout << line << std::endl; } while (!is.eof()); return true; } Well, how can I check that the file is opened correct/the stream is valid? is_open() and good() seems not supported? Or I'm wrong? Giving a wrong name does nothings. Thanks, Olaf
ope wrote:
Hi,
I've the following code:
namespace io = boost::iostreams;
bool foo::open( const std::string& file ) { io::filtering_istream is;
is.push( my_line_filter() ); is.push( io::file_source( file ) );
// ??? how to check success
do { std::string line; std::getline(is, line); std::cout << line << std::endl; } while (!is.eof());
return true; }
Well, how can I check that the file is opened correct/the stream is valid? is_open() and good() seems not supported? Or I'm wrong? Giving a wrong name does nothings.
Use file_source::is_open() (http://tinyurl.com/29o2wm). You can call it before adding the filter to the chain: file_source src(file); if (!is_open(src)) { ... } ... is.push(src); or after adding it (http://tinyurl.com/2cmlke): is.push(my_line_filter()); is.push(file_source(file)); if (!is.component<file_source>(1).is_open()) .... Here 1 is the offset of the file_source in the filter chain.
Thanks, Olaf
-- Jonathan Turkanis CodeRage http://www.coderage.com
Jonathan Turkanis wrote:
if (!is.component<file_source>(1).is_open()) ....
Whoops! This should be if (!is.component<file_source>(1)->is_open()) -- Jonathan Turkanis CodeRage http://www.coderage.com
participants (2)
-
Jonathan Turkanis
-
ope