该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
问个问题,写了一个小程序如下,想测试一下wchar.h包中的宽字节函数是否正常工作,用的是mingw+code::blocks编译,用记事本输入一些文字然后保存为utf-16格式(比如说输入了6个字符),然后用fgetwc读取这个文件,按理说应该一次读取两个字节,这样get_wchar_file_length这个函数读取的字符数应该是输入的字符数加上1(uft-16文件的BOM文件标识0xfffe)应该是7个字符,可是实际运行时返回的字符数正好是这个文件的字节数14,而不是7,感觉好像fgetwc读取的是字节而不是宽字节,请问为什么?有vc的请编译一下看是不是还是这个问题,谢谢!
/***************************************************************
a program for test functions that dealing with wide characters
in the library wchar.h
****************************************************************
author: weiyuan
time: 2007/01/06
***************************************************************/
#include
#include
#include
#include
#include
long get_wchar_file_length(FILE *fp);
int main(int argc, char argv[])
{
FILE *fpin,*fpout;
char source[20];
long file_length;
/*wchar buffer for holding a wide character reading from source file*/
printf("please input source file name\n");
gets(source);
/*get source file name*/
if((fpin=fopen(source,"r"))==NULL)
{
printf("cannot read file\n");
exit(1);
}
/*open source file*/
file_length=get_wchar_file_length(fpin);
printf("length of source file is %d",file_length);
getchar();
}
long get_wchar_file_length(FILE *fp)
{
long file_length=0;
while(fgetwc(fp)!=WEOF)
file_length++;
rewind(fp);
/*set file position to origin of file*/
return file_length;
}