2017-06-09 14 views
0

ファイル内の文字列を検出しようとしています。 QStringの.containsメソッドを使用します。これは私がこれを行うと、期待どおりに動作します(QString.contains())が正しく動作しない場合は、temp bool変数を使用して修正してください。

bool result; 
QString line = in.readLine(); 
QString target("999"); 
result = line.contains(target,Qt::CaseInsensitive); 
qDebug() << result; 
if (result) 
{ 
    qDebug() << "Comment found" << line; 
    QString line = in.readLine(); 
    qDebug() << "Comment: " << line; 
} 

しかし、私はこれを行うときには、道より頻繁に一致します。

bool result; 
QString line = in.readLine(); 
QString target("999"); 
if (line.contains(target,Qt::CaseInsensitive)); 
{ 
    qDebug() << "Comment found" << line; 
    QString line = in.readLine(); 
    qDebug() << "Comment: " << line; 
} 

TESTFILE:最初のプログラムから

999 
This file is edited by ........ 
    0 
SECTION 
    2 
HEADER 
    9 
$ACADVER 
    1 
AC1014 
    9 
$ACADMAINTVER 
70 
    9 
    9 
$DWGCODEPAGE 
    3 
ANSI_1252 
    9 
$INSBASE 
10 
0.0 
20 
0.0 
30 
0.0 
    9 
$EXTMIN 
10 
1.000000000000000E+20 
20 
1.000000000000000E+20 
30 
1.000000000000000E+20 
    9 
$EXTMAX 

出力:

"O:/<<snipped>>.DXF" 
true 
Comment found "999" 
Comment: "This file is edited by ........" 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 
false 

出力frオム二プログラム:

true 
Comment found "999" 
Comment: "This file is edited by ........" 
false 
Comment found " 0" 
Comment: "SECTION" 
false 
Comment found " 2" 
Comment: "HEADER" 
false 
Comment found " 9" 
Comment: "$ACADVER" 
false 
Comment found " 1" 
Comment: "AC1014" 
false 
Comment found " 9" 
Comment: "$ACADMAINTVER" 
false 
Comment found " 70" 
Comment: "  9" 
false 
Comment found " 9" 
Comment: "$DWGCODEPAGE" 
false 
Comment found " 3" 
Comment: "ANSI_1252" 
false 
Comment found " 9" 
Comment: "$INSBASE" 
false 
Comment found " 10" 
Comment: "0.0" 
false 
Comment found " 20" 
Comment: "0.0" 
false 
Comment found " 30" 
Comment: "0.0" 
false 
Comment found " 9" 
Comment: "$EXTMIN" 
false 
Comment found " 10" 
Comment: "1.000000000000000E+20" 
false 
Comment found " 20" 
Comment: "1.000000000000000E+20" 
false 
Comment found " 30" 
Comment: "1.000000000000000E+20" 
false 
Comment found " 9" 
Comment: "$EXTMAX" 

完全なコードは:

void MainWindow::on_pushButton_clicked() 
{ 
    QString fileName; 
    fileName = QFileDialog::getOpenFileName(this,tr("Open File"),"","Dxf Files(*.dxf);;All files(*.*)"); 
    qDebug()<<fileName; 

    bool result; 
    QFile sourceFile; 
    QFile targetFile; 
    sourceFile.setFileName(fileName); 
    if (sourceFile.open(QIODevice::ReadOnly | QIODevice::Text)) 
    { 
     QTextStream in(&sourceFile); 
     while (!in.atEnd()) 
     { 
      QString line = in.readLine(); 
      QString target("999"); 
      result = line.contains(target,Qt::CaseInsensitive); 
      qDebug() << result; 
      if (line.contains(target,Qt::CaseInsensitive)); //is true for all strings with numbers? 
      //if (result) 
      { 
       qDebug() << "Comment found" << line; 
       QString line = in.readLine(); //does this construction crash when the input file contains 999 as the last line of the file? 
       qDebug() << "Comment: " << line; 
      } 
     } 
     sourceFile.close(); 
    } 
    else 
    { 
     qDebug()<<fileName << " could not be opened"; 
    } 
} 

私が間違ってやっていますか?私は完全なスニペットでは7 敬具、 セドリック

+2

あなたは誤字があります。この行の最後にあるセミコロンは、 'if(line.contains(target、Qt :: CaseInsensitive));'は 'if'ステートメントを無効にします。 –

+3

'' Qt :: CaseInsensitive'は '' 999 "'と比較すると無用です。 –

+1

なぜCの作成者は、空の文を許可するのが良い考えだと思っているのでしょうか? (はい、私はこれで噛まれて、誰もいないですか?) – stefaanv

答えて

3

Windows上でのQt 5.7.0実行、あなたが行の末尾にセミコロン;を持っている:

if (line.contains(target,Qt::CaseInsensitive)); 

その結果、if文はありません何も表示されず、次のブロックが常に実行されます。

{ 
    qDebug() << "Comment found" << line; 
    QString line = in.readLine(); //does this construction crash when the input file contains 999 as the last line of the file? 
    qDebug() << "Comment: " << line; 
} 
関連する問題