string -> QString
1 std::string strStd = "中文字符";
2 QString strQ = QString::fromLocal8Bit(strStd.c_str());
QString -> string
1 QString strQ("中文字符");
2 std::string strStd= strQ.toStdString();
3 QTextCodec *code = QTextCodec::codecForName("gb18030");
4 // 如果code为0,表示在运行的机器上没有装gb18030字符集。不过一般的中文windows系统中都安装了这一字符集
5 if (code) strStd= code->fromUnicode(strQ).data();
有时候用ifstream或ofstream打开带有中文路径的文件会失败。
解决办法:
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
当然选2啦!
1.最近编程采用C 文件操作来读入一个文件,代码:
ifstream inf;
inf.open(strpath); //通过文件路径打开文件
当文件路径strpath中带有中文时程序运行错误,导致卡死,后来终于弄清用C 方法打开带中文路径的文件时会有BUG,谁叫这东东是老外写的呢,但是通过下面的方法可以解决,如代码:
ifstream inf; //C 方式打开文件
locale::global(locale("")); //将全局区域设为操作系统默认区域
inf.open(strpath); //通过文件路径打开文件
locale::global(locale("C")); //还原全局区域设定string -> QString
1 std::string strStd = "中文字符";
2 QString strQ = QString::fromLocal8Bit(strStd.c_str());
QString -> string
1 QString strQ("中文字符");
2 std::string strStd= strQ.toStdString();
3 QTextCodec *code = QTextCodec::codecForName("gb18030");
4 // 如果code为0,表示在运行的机器上没有装gb18030字符集。不过一般的中文windows系统中都安装了这一字符集
5 if (code) strStd= code->fromUnicode(strQ).data();
有时候用ifstream或ofstream打开带有中文路径的文件会失败。
解决办法:
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
当然选2啦!
1.最近编程采用C 文件操作来读入一个文件,代码:
ifstream inf;
inf.open(strpath); //通过文件路径打开文件
当文件路径strpath中带有中文时程序运行错误,导致卡死,后来终于弄清用C 方法打开带中文路径的文件时会有BUG,谁叫这东东是老外写的呢,但是通过下面的方法可以解决,如代码:
ifstream inf; //C 方式打开文件
locale::global(locale("")); //将全局区域设为操作系统默认区域
inf.open(strpath); //通过文件路径打开文件
locale::global(locale("C")); //还原全局区域设定
联系客服