2013-05-29 52 views
0

我需要解析vol命令的輸出才能得到id,即只有abcd-1234,即在QProcess中使用。這裏是我的代碼,以獲得卷序列號:在Qt中解析輸出

QProcess process; 
process.start("cmd /c vol C:"); 
process.waitForFinished(-1); 
QByteArray out = process.readAllStandardOutput(); 
qDebug() << out; 

幫助我,謝謝...

+0

你可以嘗試稍微用力 –

回答

1

您可以使用QRegExp(http://qt-project.org/doc/qt-4.8/qregexp.html)正則表達式查找ID。 vol命令將始終返回相同的消息。所以,你可以逐行讀取結果行和搜索匹配的行:

QRegExp rx("The Volume Serial Number is (.+)\\."); // Match the line with the ID and store it. 
if (rx.exactMatch(line) { 
    QString id = rx.capturedTexts(1); // The first elt is the entire matching text. 
    qDebug() << id; 
} 
+0

非常感謝,u能PLZ在我上面的代碼中添加此? – highlander141