如何从Qt中的txt文件中删除一行?
How to delete a line from a txt file in Qt?
我的 txt 文件 (CopyBook.txt) 包含例如 10 行。我要删除第三个
我有这个代码:
QString fname = "C://Users//Tomahawk//Desktop//copy//CopyBook.txt";
QFile file(fname);
if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
{
QTextStream edit(&file);
QString line;
int reachForLine = 0;
int neededLine = 3;
while (reachForPage != pageCounter)
{
line = edit.readLine();
reachForPage++;
}
}
所以你可以看到我使用“while”来找到我想删除的行。但是我还没有在 Qt 中找到任何允许我这样做的方法。将来我想使用删除行的功能来替换它们。那么如何删除呢?
一种方法是将所有行读入 QStringList
,修改 QStringList
,然后返回并将其内容再次写回文件,就像这样:
int main(int argc, char ** argv)
{
const QString fname = "C:/Users/Tomahawk/Desktop/copy/CopyBook.txt";
QStringList lines;
// Read lines of text from the file into the QStringList
{
QFile inputFile(fname);
if (inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream edit(&inputFile);
while (!edit.atEnd()) lines.push_back(edit.readLine());
}
inputFile.close();
}
// Delete the third line from the QStringList
if (lines.length() > 2) lines.removeAt(2); // 0==first line, 1==second line, etc
// Write the text in the QStringList back to the file
{
QFile outputFile(fname);
if (outputFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream edit(&outputFile);
for (int i=0; i<lines.size(); i++) edit << lines[i] << Qt::endl;
}
outputFile.close();
}
return 0;
}
您还可以在 QStringList
对象上执行任何 replacements/insertions,然后再将其写回文件。
请注意,此方法确实会使用与文件大小成比例的 RAM,因此对于非常大的文件(例如千兆字节长),您可能希望使用创建第二个文件然后-相反,@TedLyngmo 在他的评论中提出的重命名方法。对于小文件,OTOH,在 RAM 中缓冲更容易,也更不容易出错。
我的 txt 文件 (CopyBook.txt) 包含例如 10 行。我要删除第三个
我有这个代码:
QString fname = "C://Users//Tomahawk//Desktop//copy//CopyBook.txt";
QFile file(fname);
if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
{
QTextStream edit(&file);
QString line;
int reachForLine = 0;
int neededLine = 3;
while (reachForPage != pageCounter)
{
line = edit.readLine();
reachForPage++;
}
}
所以你可以看到我使用“while”来找到我想删除的行。但是我还没有在 Qt 中找到任何允许我这样做的方法。将来我想使用删除行的功能来替换它们。那么如何删除呢?
一种方法是将所有行读入 QStringList
,修改 QStringList
,然后返回并将其内容再次写回文件,就像这样:
int main(int argc, char ** argv)
{
const QString fname = "C:/Users/Tomahawk/Desktop/copy/CopyBook.txt";
QStringList lines;
// Read lines of text from the file into the QStringList
{
QFile inputFile(fname);
if (inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream edit(&inputFile);
while (!edit.atEnd()) lines.push_back(edit.readLine());
}
inputFile.close();
}
// Delete the third line from the QStringList
if (lines.length() > 2) lines.removeAt(2); // 0==first line, 1==second line, etc
// Write the text in the QStringList back to the file
{
QFile outputFile(fname);
if (outputFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream edit(&outputFile);
for (int i=0; i<lines.size(); i++) edit << lines[i] << Qt::endl;
}
outputFile.close();
}
return 0;
}
您还可以在 QStringList
对象上执行任何 replacements/insertions,然后再将其写回文件。
请注意,此方法确实会使用与文件大小成比例的 RAM,因此对于非常大的文件(例如千兆字节长),您可能希望使用创建第二个文件然后-相反,@TedLyngmo 在他的评论中提出的重命名方法。对于小文件,OTOH,在 RAM 中缓冲更容易,也更不容易出错。