尝试打开并读取 .txt 文件,但它会从原始文件中删除实际文本(Qt GUI C++
Trying to open and read a .txt file, but it deletes the actual text from the original file (Qt GUI C++
我试图打开并读取一个 .txt 文件,然后在标签中显示其中的一行,但该行未显示,但所有内容都已从原始选定文件中删除。有什么想法吗?
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open File"),
"/Users/kat/Documents/Programming 2/Assignment/Quizzes",
"All Files (*.*);;Text File (*.txt)");
QFile file(filename);
if(!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(this, tr("File Not Open"),"File Cannot Be Opened");
}
QTextStream in(&file);
QRegularExpression re("Name: ");
QString title = in.readLine();
title.remove(re);
ui->label_4->setText(title);
file.flush();
file.close();```
您已打开文件进行写入而不是读取 if(!file.open(QFile::WriteOnly | QFile::Text))
。如果你需要读取一个文件,你不应该做 file.flush()
.
我试图打开并读取一个 .txt 文件,然后在标签中显示其中的一行,但该行未显示,但所有内容都已从原始选定文件中删除。有什么想法吗?
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open File"),
"/Users/kat/Documents/Programming 2/Assignment/Quizzes",
"All Files (*.*);;Text File (*.txt)");
QFile file(filename);
if(!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(this, tr("File Not Open"),"File Cannot Be Opened");
}
QTextStream in(&file);
QRegularExpression re("Name: ");
QString title = in.readLine();
title.remove(re);
ui->label_4->setText(title);
file.flush();
file.close();```
您已打开文件进行写入而不是读取 if(!file.open(QFile::WriteOnly | QFile::Text))
。如果你需要读取一个文件,你不应该做 file.flush()
.