[Boost-users] boost::iostreams : equivalent of std::getline() in boost

2,462 views
Skip to first unread message

vandamme

unread,
Feb 22, 2009, 11:31:11 AM2/22/09
to boost...@lists.boost.org

Hello !

I want to use boost to read a text file like this :

boost::iostreams::file_source file(filename.c_str());

But, when I want to get each single line with getline(), it doesn't compile
:

while (std::getline(file, line)) { ...} // COMPILATION ERROR

What's the equivalent in boost for std::getline() ?

Thks.
--
View this message in context: http://www.nabble.com/boost%3A%3Aiostreams-%3A-equivalent-of-std%3A%3Agetline%28%29-in-boost-tp22148618p22148618.html
Sent from the Boost - Users mailing list archive at Nabble.com.

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Steven Watanabe

unread,
Feb 22, 2009, 2:14:06 PM2/22/09
to boost...@lists.boost.org
AMDG

vandamme wrote:
> I want to use boost to read a text file like this :
>
> boost::iostreams::file_source file(filename.c_str());
>
> But, when I want to get each single line with getline(), it doesn't compile
> :
>
> while (std::getline(file, line)) { ...} // COMPILATION ERROR
>
> What's the equivalent in boost for std::getline() ?
>

You can use boost::iostreams::stream.

#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <istream>

int main() {
std::string filename("test");
boost::iostreams::stream<boost::iostreams::file_source>
file(filename.c_str());
std::string line;
while (std::getline(file, line)) { }
}

In Christ,
Steven Watanabe

vandamme

unread,
Feb 22, 2009, 3:04:17 PM2/22/09
to boost...@lists.boost.org

Thanks for your answer.

Still, I have a strange behavior :
When the file doesn't exist, file.is_open() still return true !!!

When I use 'ifstream file' instead of
'boost::iostreams::stream<boost::iostreams::file_source> file',
the test 'if (file)' is always wrong when the file doesn't exist.

--
View this message in context: http://www.nabble.com/boost%3A%3Aiostreams-%3A-equivalent-of-std%3A%3Agetline%28%29-in-boost-tp22148618p22150873.html


Sent from the Boost - Users mailing list archive at Nabble.com.

_______________________________________________

Reply all
Reply to author
Forward
0 new messages