C++使用WideCharToMultiByte函数生成UTF-8编码文件的方法

  FILE * pFile = fopen("d://a.txt", "w");

  char szBOM[4] = {(char)0xEF, (char)0xBB, (char)0xBF, 0};

  fprintf(pFile, "%s", szBOM);

  wchar_t chNum[11] = L"零壹贰叁肆伍陆柒捌玖";

  wchar_t chNum2[10] = L"亿千百拾万千百拾个";

  char sz[10] = "112304823";

  wchar_t result[32] = L"";

  int offset = 0;

  for(int i = 0; i < strlen(sz); ++ i)

  {

  char c = sz[i];

  wchar_t w1 = chNum[ c - '0'];

  wchar_t w2 = chNum2[i];

  swprintf(result + offset, L"%c%c", w1, w2);

  offset += 2;

  }

  char szChar[64] = "";

  ::WideCharToMultiByte(CP_UTF8, 0, result, wcslen(result), szChar, 64, 0, 0);

  fprintf(pFile, "%s", szChar);

  fclose(pFile);