FILE *pf = fopen(("d:\\33.txt"),("wb,ccs=UTF-8"));
if(pf == NULL)
{
return;
}
TCHAR cc[10]=_T("中国");
//_ftprintf(pf, _T("%s\n"), cc);
int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, cc, wcslen(cc), NULL, 0, NULL, NULL);
char* szU8 = new char[u8Len + 1];
//转换
//unicode版对应的strlen是wcslen
::WideCharToMultiByte(CP_UTF8, NULL, cc, wcslen(cc), szU8, u8Len, NULL, NULL);
//最后加上'\0'
szU8[u8Len] = '\0';
fprintf(pf, ("%s\r\n"),szU8);
fprintf(pf, ("%s\r\n"),szU8);
delete szU8;
fclose(pf);
FILE *pf1 = fopen(("d:\\33.txt"),("r"));
if (pf1 == NULL)
{
return;
}
char ch[1024];
fscanf(pf1, "%s\n", ch);
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, ch, strlen(ch), NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_UTF8, NULL, ch, strlen(ch), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0';
fscanf(pf1, "%s\n", ch);
wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, ch, strlen(ch), NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
// wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_UTF8, NULL, ch, strlen(ch), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0';
delete []wszString;
fclose(pf1);
创建utf-8文件,并且向里面写数据,在读出来
最新推荐文章于 2024-05-27 17:40:49 发布