删除单行文本文件QT
Delete single line in textfile QT
我有一个文本文件,看起来像这样(示例):
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
,但是我怎么做呢?
如果文件可以很大:
- 创建临时文件
- 在读取时,将除要删除的行外的所有行写入临时文件。
- 删除原文件
- 将临时文件重命名为原始文件
如果你知道文件总是很小,你可以选择这样做:
- 在内存中读取所有行,除了要删除的行。
- 使用你现在在内存中的字符串重写文件。
相关文章:
- 如何在cpp文件之间切换窗口?在Qt中
- Qt 5.11.2 (Clang 8.0 (Apple), 64 位), 找不到 QJSEngine 文件
- 使用 Bazel 生成 QT 资源文件
- Qt C++:不同文件中同一命名空间的多个Q_NAMESPACE
- 如何识别项目是 QT 中的文件还是文件夹
- 使用 Cmake 在 Qt Creator 中显示头文件
- 如何将网络模块添加到 QT pro 文件中?
- 如何在 qt for windows 中使用 QAxObject 创建 docx 和 doc 文件?
- 如何将 json 数据附加到 Qt 中的现有 json 文件中
- 如何将Qt文件路径从资源转换为绝对路径?
- 如何在 Qt 文件流中使用 >> 运算符 while 循环
- 打开QT文件不会创建文件
- 来自 Qt 文件的编译时错误:令牌之前的预期非限定 id ')'
- qt文件读取行在第二次读取时总是返回-1
- 编译时找不到Qt文件
- 使用组件找不到Qt文件
- 设置dll路径来构建qt文件
- 从Qt文件中读取整数
- QT:文件存储在appdata下的VirtualStore文件夹中
- libopencv_java3.在qt文件中