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

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值