Overview
Features
Download
Documentation
Community
Add-Ons & Services

InputStreamConverter

A general discussion forum.

InputStreamConverter

Postby cdelmas » 06 Mar 2007, 16:39

Hi all,
I have a file encoded using utf16. I want to convert the content in Latin1 encoding, so I try to do it using InputStreamConverter. As you guess, that doesn't work :

- - - - - - - - - - - - -

ifstream file("C:\APutf-16.csv", fstream::binary);

UTF16Encoding utf16Encoding;

Latin1Encoding latin1Encoding;

InputStreamConverter converter(file,utf16Encoding,latin1Encoding,'°');

cout << "XXXXXXXXXX UTF16 to LATIN1 conversion XXXXXXXXX" << endl << endl;

stringstream stream;

stream << converter.rdbuf();

cout << stream.str() << endl;

- - - - - - - - - - - - -

The output prints an empty string. I suppose I'm wrong somewhere, but I don't know where. Can anybody help me ?

Thanks in advance.
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: InputStreamConverter

Postby cdelmas » 06 Mar 2007, 16:42

Well, I posted too fast. I read the doc again and understood what's wrong. The buffer converts the characters on-the-fly while reading the input stream. My apologies.

- - - - - - - - -

ifstream file(filename,fstream::binary);

InputStreamConverter converter(file,in,out,'°');

stringstream stream;

stream < < converter.rdbuf();

string buffer;

std::getline(converter,buffer);

- - - - - - - - - -

However, I don't understand why without the fourth line it doesn't work...
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: Re: InputStreamConverter

Postby guenter » 09 Mar 2007, 19:11

The following line
Code: Select all

stream < < converter.rdbuf();


definitely makes no sense.rdbuf() returns a pointer to the StreamBuf, which makes absolutely no sense to write to a stream.


The following sample works fine for me:

Code: Select all

#include "Poco/UTF16Encoding.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Latin1Encoding.h"
#include "Poco/StreamConverter.h"
#include "Poco/StreamCopier.h"
#include
#include


int main(int argc, char** argv)
{
   Poco::UTF16Encoding utf16;
   Poco::Latin1Encoding latin1;
   
   std::ifstream istr("c:\utf16.txt", std::ios::binary);
   Poco::InputStreamConverter is(istr, utf16, latin1);
   Poco::StreamCopier::copyStream(is, std::cout);
   
   return 0;
}


and this one, too

Code: Select all

#include "Poco/UTF16Encoding.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Latin1Encoding.h"
#include "Poco/StreamConverter.h"
#include "Poco/StreamCopier.h"
#include
#include


int main(int argc, char** argv)
{
   Poco::UTF16Encoding utf16;
   Poco::Latin1Encoding latin1;
   
   std::ifstream istr("c:\utf16.txt", std::ios::binary);
   Poco::InputStreamConverter is(istr, utf16, latin1);

   std::string buffer;
   std::getline(is, buffer);
   std::cout < < buffer;

   return 0;
}
guenter
 
Posts: 1129
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest