linux c++、Qt中的system()参数

parameter of system() in linux C++,Qt

本文关键字:参数 system 中的 c++ Qt linux      更新时间:2023-10-16

当我使用长字符串(其中包含一些中文字符)调用system()时,

system()似乎没有正确处理我的参数

。system()收到的和我发送的不一样

//it based on Qt
void work(QString order)
{
   system((const char*)order.toLocal8Bit());
   // in terminal, it shows a wrong command different with what it should be.
}

当我调用

work( "g++ "+nfile+name+".cpp -o "+nfile+name+" 2>"+nfile+"compiler.out" );

nfile表示带有一些中文字符

路径。

如果您正在使用Qt,那么最好使用QProcess而不是system,请参阅此处

将字符串转换为UTF-8并传递给system():

void work(const QString &order)
{
   system(order.toUtf8().constData());
}

根据toLocal8Bit()的文档

返回的字节数组未定义如果字符串包含非支持本地8位编码

我假设你正在使用的中文字符是不支持的。您可以尝试使用toUtf8。