IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ qt 使用 ifstream 读取路径中带中文的文件失败解决 -> 正文阅读

[C++知识库]C++ qt 使用 ifstream 读取路径中带中文的文件失败解决

windows下 前端是js 选择 文件,文件名带中文,编码utf-8。

后端是C++,接收到的是乱码,导致文件打开失败,找解决办法尝试:

1、QString::fromLocal8Bit()

Qt5 加载中文路径以及中文文本显示乱码问题 - 码农教程

2、使用_TEXT()宏定义将字符串常量指定为TCHAR*类型

3、locale::global(locale(""));//将全局区域设为操作系统默认区域

4、setlocale(LC_ALL,"Chinese-simplified");//设置中文环境

C++读取路径中带中文的文件_ajax000的专栏-CSDN博客

以上解决办法尝试都不行,不知道原因。

以下测试可行方法:

1、可行,但是打包需要引库 Qt5Cored.dll? 比较大

#include<QTextCodec>


string selectPath = "C:\\Users\\Administrator\\Desktop\\t_out中文\\out.txt";
cout << "selectPath== " << selectPath << endl;

QTextCodec *code = QTextCodec::codecForName("GB2312");
string selectedFile = code->fromUnicode(selectPath.c_str()).data();

cout << "selectedFile== " << selectedFile << endl;

//ifstream isd;
//isd.open(selectedFile);

ifstream isd(selectedFile);
char data[5];
isd.read(data, 5);
isd.close();

2、最终我把字符编码转换了一下(ut8、gb2312),可以获取中文,不是乱码,就可以了

string selectPath = "C:\\Users\\Administrator\\Desktop\\t_out中文\\out.txt";
cout << "selectPath== " << selectPath << endl;

strCoding cfm;
string selectedFile="";
cfm.UTF_8ToGB2312(selectedFile,(char *)selectPath.data(),strlen(selectPath.data()));
cout << "selectedFile== " << selectedFile << endl;

//ifstream isd;
//isd.open(selectedFile);

ifstream isd(selectedFile);
char data[5];
isd.read(data, 5);
isd.close();
void strCoding::UTF_8ToUnicode(WCHAR* pOut,char *pText)
{
    char* uchar = (char *)pOut;

    uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
    uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);

    return;
}

void strCoding::UnicodeToGB2312(char* pOut,WCHAR uData)
{
    WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);
    return;
}

//UTF_8 转gb2312
void strCoding::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
     char buf[4];
     char* rst = new char[pLen + (pLen >> 2) + 2];
    memset(buf,0,4);
    memset(rst,0,pLen + (pLen >> 2) + 2);

    int i =0;
    int j = 0;

    while(i < pLen)
    {
        if(*(pText + i) >= 0)
        {

            rst[j++] = pText[i++];
        }
        else
        {
            WCHAR Wtemp;


            UTF_8ToUnicode(&Wtemp,pText + i);

            UnicodeToGB2312(buf,Wtemp);

            unsigned short int tmp = 0;
            tmp = rst[j] = buf[0];
            tmp = rst[j+1] = buf[1];
            tmp = rst[j+2] = buf[2];

            //newBuf[j] = Ctemp[0];
            //newBuf[j + 1] = Ctemp[1];

            i += 3;
            j += 2;
        }

  }
    rst[j]='\0';
   pOut = rst;
    delete []rst;
}

原文参考链接:

QT utf8编码转gb2312编码,互相转换的源代码_AmyAndTommy的博客-CSDN博客_utf8转gb2312代码

C/C++ 字符编码的转换(ut8、gb2312) - 戏梦 - 博客园

Qt中使用fopen打开文件(路径有中文时)失败解决_zcc的博客-CSDN博客#include<QTextCodec> //引入string bmpName = "C:\\Users\\Administrator\\Desktop\\testsubtitle中文.bmp";QTextCodec *code = QTextCodec::codecForName("GB2312");std::string name = code->fromUnicode(bmpName.c_str()).data();FILE *fp = fopen(name.c_st.https://blog.csdn.net/qq_40015157/article/details/119142724

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-18 09:54:07  更:2021-09-18 09:59:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 0:16:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码