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
  • 相关阅读:
    chapter02“良/恶性乳腺癌肿瘤预测”的问题
    ASCII编码和Unicode编码的区别
    Spring AOP概述
    Spring 基于注解的配置
    Spring Bean作用域&FactoryBean
    Spring <bean> 之间的关系&整合多个配置文件
    Spring 方法注入
    Spring 简化装配Bean的配置方式
    Spring 注入参数详解
    vue-router 导航守卫
  • 原文地址:https://www.cnblogs.com/findumars/p/6375852.html
Copyright © 2011-2022 走看看