[LC++] Unicode writing problem

David Nugent davidn at datalinktech.com.au
Wed Jan 7 08:55:04 EST 2009

Hash: SHA1

I will preface this with a disclaimer that I have not worked  
extensively with the stdlib wchar_t and wstring. When I have used  
them, they seemed to work as expected and would usually be used one  
would use char_t and std::string in a UTF-8 environment.

On 06/01/2009, at 9:57 PM, Radhika M G wrote:

> Hi,
> I’m trying to create a csv file in Unicode format. I’m using  
> wofstream to create the file.

Which unicode format? 8, 16 or 32 bit?

> I’m writing a series of strings and double values to the stream.
> I find that the values written to wofstream stop after writing 9  
> sets of data. There are about 37 sets of data.
> What could be the issue?
> The code that writes data to wofstream is as below:
> for ( int i=0; i<str.length(); i++ )
>  {
>     wchar_t wc = ( str[i]<<16 );

Why the str[i] << 16?  AFAIK, str[i] would automatically access the  
code point correctly and place it into a wchar_t without any bit  
shifting. The result of doing this would be non-portable in any case  
without using a BOM. It is certainly the wrong thing to do for either  
UTF-8 and UTF-16.

>     os << wc;
> //     os.seekp( -1, ios_base::cur );

??  Why are you doing this?

> }
> Where
> str:  wstring.
> os: wofstream
> gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)
> The above code is called 30-40 times for different values of str.
> If I uncomment os.seekp() statement,  the file has no value.

Explain "no value"? Is it empty, aka zero length? or is it full of nuls?

> os.fail() is not showing any error. Unable to figure where things  
> are failing.
> I’m able to output contents of str on standard ouput. But not able  
> to write to file completely.

It may be worth noting that standard output does not support seek, and  
nul would be invisible on the console.


Version: GnuPG v1.4.8 (Darwin)


More information about the tuxCPProgramming mailing list