简体   繁体   English

Linux C ++,Qt中system()的参数

[英]parameter of system() in linux C++,Qt

when I call system() with a long string(and it contains some Chinese characters), 当我用长字符串(包含一些汉字)调用system()时,

system() seems not to deal my parameter correctly. system()似乎无法正确处理我的参数。

eg what system() recieved was not the same with what I sent 例如,收到的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.
}

and when i call 当我打电话时

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

nfile represents a long path with some Chinese characters nfile表示带有一些汉字的路径

Convert the string to UTF-8 and pass that to system() : 将字符串转换为UTF-8并将其传递给system()

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

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

According to the documentation for toLocal8Bit() 根据toLocal8Bit()文档

The returned byte array is undefined if the string contains characters not supported by the local 8-bit encoding. 如果字符串包含本地8位编码不支持的字符,则返回的字节数组是不确定的。

I'm assuming the Chinese characters you're using aren't supported. 我假设不支持您使用的中文字符。 You may wish to try toUtf8 instead. 您不妨尝试使用totf8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM