#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
int main()
{
//把"这是Unicode编码"用Unicode编码写入一个文件
FILE *file = fopen("test.txt","wb");
wchar_t wstr[] = L"这是Unicode编码";
wchar_t strSign = 0xfeff;
int len = wcslen(wstr);
fwrite(&strSign,1,2,file);
fwrite(wstr,1,len*2,file);
fclose(file);
//打开这个文件,并且读出其中的数据显示
file = fopen("test.txt","rb");
int nFileLen;
wchar_t buffer[100] = {0}; //注意,文件不要超过99个字符
if(file)
{
fseek(file,0,SEEK_END);
nFileLen = ftell(file);
fseek(file,0,SEEK_SET);
fread(buffer,1,nFileLen,file);
}
else
{
printf("file does not exists.\n");
getchar();
return 0;
}
if(buffer[0] == 0xfeff)
{
printf("it's a Unicode file\n");//is unicode
char strBuffer[200]={0};
if (!setlocale(LC_ALL, "chs"))
{
perror("chs");
getchar();
return 0;
}
wcstombs(strBuffer,&buffer[1] , 200);
printf("%s",strBuffer);
}
else
printf("it's not a Unicode file\n");//not unicode
fclose(file);
getchar();
return 0;
}
问题很简单,一个文件是Unicode编码。请帮忙写一个完整示例,读入并判断其中的字符是否是"这是Unicode编码"。写成控制台输出Yes/No的程序即可。在vc编译通过.