gpt4 book ai didi

c++ - 如果 (QString.contains()) 不能正常工作,使用 temp bool 变量更正

转载 作者:行者123 更新时间:2023-11-27 23:53:26 28 4
gpt4 key购买 nike

<分区>

我正在尝试检测文件中的字符串。我使用 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;
}

测试文件:

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亲切的问候,塞德里克

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com