我有一个文本文件,看起来像这样(示例):
123456789 18-5-2014
985665547 23-12-2016
我在while(!file.atEnd)
结构中有一个这样的读/写函数:
while (!file.atEnd())
{
if (date-currentdate<42) {
ui->label->setText(number); //number is the number before the date in the text file
//Here I want the function to delete the current line
}
}
我想要的是从我的文本文件中删除刚刚在if
语句中使用的行。我说的行(例如)是指123456789 15-5-2014
,但是我怎么做呢?
如果文件可以很大:
- 创建临时文件
- 在读取时,将除要删除的行外的所有行写入临时文件。
- 删除原文件
- 将临时文件重命名为原始文件
如果你知道文件总是很小,你可以选择这样做:
- 在内存中读取所有行,除了要删除的行。
- 使用你现在在内存中的字符串重写文件。