when I call system() with a long string(and it contains some Chinese characters),
system() seems not to deal my parameter correctly.
e.g. what system() recieved was not the same with what I sent
//it ba开发者_运维技巧sed 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
If you are using Qt, then its better to use QProcess
rather than system
, see here
Convert the string to UTF-8 and pass that to system()
:
void work(const QString &order)
{
system(order.toUtf8().constData());
}
According to the documentation for toLocal8Bit()
The returned byte array is undefined if the string contains characters not supported by the local 8-bit encoding.
I'm assuming the Chinese characters you're using aren't supported. You may wish to try toUtf8 instead.
精彩评论