Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
2.00/5 (1 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:
C++
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
Updated 15-Jan-12 5:43am
v4
Comments
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.

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

Bye
 
Share this answer
 
Comments
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
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.
 
Share this answer
 
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:
C++
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
or
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:
C++
int _tmain(int argc, _TCHAR* argv[])
{
	wchar_t a[3]=L"ab";
	char *b;
	
	b=(char*)(a);
	return 0;
}


if you debug you would see the stored character sequence is like below:
C++
b[0]='a'
b[1]=0
b[2]='b'
b[3]=0
b[4]=0
b[5]=0


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
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900