Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Win7 Unicode
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 5:44am
Edited 15-Jan-12 6:43am
v4
Comments
Richard MacCutchan at 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
good
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.
 
Bye
  Permalink  
Comments
Richard MacCutchan at 15-Jan-12 12:43pm
   
Tried that and it doesn't work.
devetslova at 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
good
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.
  Permalink  
Rate this: bad
good
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
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:
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:
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
  Permalink  
v3

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

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 273
2 Aajmot Sk 224
3 Marcin Kozub 210
4 Richard MacCutchan 200
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 16 Jan 2012
Copyright © CodeProject, 1999-2014
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