Page 1 of 1

how to use poco to read text files

Posted: 07 Jul 2008, 14:53
by dagg
I've tried with File, FileIOS, FileInputStream, FileOutputStream, FileStream, CountingIOS, CountingInputStream and CountingOutputStream
but no go, I'm pretty sure it exists, any help will be appriciated.

Re: how to use poco to read text files

Posted: 07 Jul 2008, 15:16
by peter
> I've tried with File, FileIOS, FileInputStream, FileOutputStream, FileStream, CountingIOS, CountingInputStream and CountingOutputStream
> but no go, I'm pretty sure it exists, any help will be appriciated.

Well,

some details would be nice...

Anway, FileInputStream should be good though, only difference to a standard ofstream is
the ability to open files where the file name contains unicode chars. Note that the FileInputStream constructor expects the filename to be UTF8 encoded.

br

Peter

Re: Re: how to use poco to read text files

Posted: 08 Jul 2008, 11:02
by dagg
ok, if I use FileInputStream how can I read a line? I've tried the

Re: Re: Re: how to use poco to read text files

Posted: 08 Jul 2008, 15:26
by alex
> ok, if I use FileInputStream how can I read a line?

Code: Select all


std::string filename("C:\test.txt");
Poco::FileInputStream fis(filename);
std::string read;
fis >> read;
std::cout << read;

Re: how to use poco to read text files

Posted: 08 Jul 2008, 17:42
by guenter
Well, the >> operator will only read up to the first whitespace.
You can try:

Code: Select all


Poco::FileInputStream fis("c:\test.txt");
std::string line;
std::getline(fis, line);


On Windows, line will probably contain a CR character (ASCII 13) at the end, which you should get rid off. Or use std: :ifstream, or Poco: :FileInputStream with a Poco::LineEndingConverter, as in:

Code: Select all


Poco::FileInputStream fis("c:\test.txt");
Poco::InputLineEndingConverter lec(fis, Poco::LineEnding::NEWLINE_LF);
std::string line;
std::getline(lec, line);

Re: Re: how to use poco to read text files

Posted: 08 Jul 2008, 19:19
by dagg
> Well, the >> operator will only read up to the first whitespace.
> You can try:
>

Code: Select all


> Poco::FileInputStream fis("c:\test.txt");
> std::string line;
> std::getline(fis, line);
>

>
> On Windows, line will probably contain a CR character (ASCII 13) at the end, which you should get rid off. Or use std: :ifstream, or Poco: :FileInputStream with a Poco::LineEndingConverter, as in:
>
>

Code: Select all


> Poco::FileInputStream fis("c:\test.txt");
> Poco::InputLineEndingConverter lec(fis, Poco::LineEnding::NEWLINE_LF);
> std::string line;
> std::getline(lec, line);
>


thanks, it worked like a charm :)