zoukankan      html  css  js  c++  java
  • Qt、Vc下用fopen打开中文名字的文件(转换成Unicode后,使用_wfopen函数)

    在做一个Qt项目的时候,完成上传文件时,通过fopen打开文件用来读时发现fopen不能打开中文的文件名,自己在网查找一下,解决方法如下

    参考:http://weidaohang.org/wangluo/h/index.PHP?q=aHR0cDovL3d3dy5qYjUxLm5ldC9hcnRpY2xlLzUxOTY2Lmh0bQ%3D%3D

    代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    boolUTF8ToUnicode(constchar* UTF8, wchar_t* strUnicode)
    {
     DWORDdwUnicodeLen;    //转换后Unicode的长度
     TCHAR*pwText;      //保存Unicode的指针
    // wchar_t* strUnicode;    //返回值
     //获得转换后的长度,并分配内存
     dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,UTF8,-1,NULL,0);
     pwText = newTCHAR[dwUnicodeLen];
     if(!pwText)
     {
     returnfalse;
     }
     //转为Unicode
     MultiByteToWideChar(CP_UTF8,0,UTF8,-1,pwText,dwUnicodeLen);
     //转为CString
     wcscpy(strUnicode, pwText);
     //清除内存
     delete[]pwText;
     returntrue;
    }

    这个函数的用法如下:

    1
    2
    3
    4
    wchar_tstrUnicode[260];
    UTF8ToUnicode(streamName, strUnicode);
    FILE* fid = _wfopen(strUnicode, L"rb");
    // FILE* fid = fopen(streamName, "rb");//此为原来的方法,遇到中文不能正确打开
    适用于Qt中和Vc中
     
    http://blog.csdn.net/guoqianqian5812/article/details/45226695
  • 相关阅读:
    JavaScript中的数组
    JavaScript中的对象
    Highcharts中设置x轴为时间的写法
    CSS 选择器(基础)
    DOM节点
    平衡木蜻蜓
    python2.7 qt4
    C语言优先级
    树莓派与stm32通信
    linux下USB转串口配置
  • 原文地址:https://www.cnblogs.com/findumars/p/6375852.html
Copyright © 2011-2022 走看看