将wchar_t元素写入文件 [英] writing wchar_t element's to file

查看:155
本文介绍了将wchar_t元素写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,当我尝试向文件中的wchar_t元素的文件号写入文件时,它仅出现我所写字母的十进制值.

这是我尝试写入文件的方式:

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;
}



我实际上有一个包含两个元素的结构(一个是wchar_t letter [5]类型,另一个是double count),我想在文件的每一行中写入该struct



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

推荐答案

您的问题不太清楚.
我认为您需要wofstream而不是fstream.
尝试让我知道.

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

Bye


我已经进行了许多测试,包括:
-将默认文件设置为文本而不是二进制
-使用wfstream类型
-使用wfstream::write()函数
无济于事.据我所知,还通过代码进行跟踪,即使输入数据为Unicode,流类型也始终会产生ASCII输出.

看来您不能为此目的使用STL流,而需要使用 fwprintf()其中之一 [ ^ ]或 fwrite() [
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.


关于宽字符:

首先尝试了解如何将数据存储在wchar_t变量中.它是两个字节的长度.要容纳旧的ascii数据,其顺序如下:
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



根据您的代码,当它得到一个空字符时,它将停止写入.变量 a 在第一个字节中保留一个空字符.

两件事之一
1.使用能够写宽字符串的类

2.我不懂c ++.但是使用stdin函数时,必须将文件作为二进制文件打开.这样您就可以毫无困难地进行写作
更正序列[/编辑]

还要调试以下代码:



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
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



如果尝试使用您声明的类在文件中写入上述字符串,则在文件中将仅获得"a".因为在第二个字节中,它的获取字符串终止字符.
注意:对于宽字符串,终止字符是两个空值的序列.但当然是array [even_index]和array [even_index + 1],而不是array [odd_index]和array [odd_index + 1].例如array [0]和array [1],但不包括array [1]和array [2].这里的数组是单字节字符串



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


这篇关于将wchar_t元素写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆