Click here to Skip to main content
12,999,628 members (60,572 online)
Rate this:
Please Sign up or sign in to vote.
Hi guys, I have a problem when I try to write to the file number of wchar_t elements in the file it only appears decimal value of the letters that I wrote.

Here is how I trying to write to file:
string file_name = name;
fstream output (file_name.c_str(), ios::binary|ios::out);
for(i=0; i<sizeof(n); i++)
    for(j=0; j<brojslova; j++)
        output<<i+1<<".\t"<< static_cast<wchar_t> (stats[i].letter[j]) << '\t' <<stats[i].count/sum_letter<<endl;

I have actually a struct of two element (one is type wchar_t letter[5], and other is double count), and i want to write in every line of file, a one element of that struct
Posted 15-Jan-12 4:44am
Updated 15-Jan-12 5:43am
Richard MacCutchan 15-Jan-12 11:07am
Then you must be doing something wrong. Show us your code, the results you expect, and the results you see.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your question is not so clear.
I think you need wofstream instead of fstream.
Try and let me know.

Richard MacCutchan 15-Jan-12 12:43pm
Tried that and it doesn't work.
devetslova 15-Jan-12 13:00pm
Tried me too, and it work with ASCII letter, and when come to unicode letter it stop writing
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I have run a number of tests including:
- set the default file to to text rather than binary
- use the wfstream type
- use the wfstream::write() function
to no avail. Also tracing through the code, as far as I can ascertain, the stream types always produce ASCII output even when the input data is Unicode.

It would appear that you cannot use the STL streams for this purpose and will need to use one of the fwprintf()[^] or fwrite()[^] functions instead.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

about wide character:

First try to understand how data is stored in a wchar_t variable. Its a two byte length. To accommodate old ascii data the sequence is like below:
wchar_t a;
a='A'; //This is correct Syntax
a=L'A';//This is correct Syntax too
//the above value in byte stored as [0]=65 [1]=0

According to your code it will stop writing when it gets a null character. and variable a holds a null character in the first byte.

either of two things
1. Use a class which is capable of writing wide string
2. I dont know c++. but using stdin function where file must have to be open as binary. and you can do your writing without trouble
[Edit]Correcting Sequence[/Edit]

Also Debug the code below:
int _tmain(int argc, _TCHAR* argv[])
	wchar_t a[3]=L"ab";
	char *b;
	return 0;

if you debug you would see the stored character sequence is like below:

if you try to write the above string in a file using the class you have stated then you will get only "a" in the file. because in the second byte its getting string terminating character.
Note: for wide character string terminating character is sequence of two null value. but of course array[even_index] and array[even_index+1], not array[odd_index] and array[odd_index+1]. as example array[0] and array[1] but not array[1] and array[2]. here array is single byte string

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 16 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100