我正在尝试检测文件中的字符串。我使用QString中的.contains方法。当我这样做时,这可以正常工作:
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
第二个程序的输出:
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";
}
}
我做错了什么?我在Windows 7上运行Qt 5.7.0 亲切的问候, 塞德里克
答案 0 :(得分:3)
在完整代码段中,您在行尾有一个分号;
:
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;
}