c++ 文件utf-8格式

 

#include <stdio.h>

 

  int i = 0;
 while (i < 20)
 {
    i++;
    WriteLog("d:\\log.txt","aaaa中国人民");
 }

 

/utf-8格式
//WriteLog("d:\\log.txt", "aaaa中国人民");
void WriteLog(char* file_path,string s)
{
    s = s + "\r\n";//换行


    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);


    
    FILE* fp = fopen(file_path, "a,ccs=UTF-8");

    fwrite(buf, sizeof(wchar_t), wcslen(buf), fp);


    fclose(fp);
    delete[]buf;

}

 

posted @ 2015-06-03 14:39  ike_li  阅读(3288)  评论(0编辑  收藏  举报