使用 C++ 删除文件中的条目
Removing an entry inside a file using C++
我在这个文件中使用 CPP 中的 fstream 命令创建了一个 txt 文件,我插入了学生的姓名,但是如果我想删除特定学生的信息而不是整个文件,是否可能以及如何删除..提前致谢...我不想跳到一行,但我想删除特定行
这里有一些方法:
- 将所有"good"条记录写入一个新文件,忽略删除的内容。
- 用“\0”标记(覆盖)文本以指示已删除的记录。
- 以read/write打开文件并移动删除后的所有文本
文本,以便它覆盖已删除的文本(想想在
数组).
删除文本或数据时文件不会自动缩小;它们保持相同的大小(将它们视为数组)。
1. Read all record from file one by one
2. if (read_record==record_to be deleted)
skip
3. else
write record into new temporary file
4. Repeat step from 2 to 3 till all file is read
5. now delete original file
6. rename temporary file to original name
我在这个文件中使用 CPP 中的 fstream 命令创建了一个 txt 文件,我插入了学生的姓名,但是如果我想删除特定学生的信息而不是整个文件,是否可能以及如何删除..提前致谢...我不想跳到一行,但我想删除特定行
这里有一些方法:
- 将所有"good"条记录写入一个新文件,忽略删除的内容。
- 用“\0”标记(覆盖)文本以指示已删除的记录。
- 以read/write打开文件并移动删除后的所有文本 文本,以便它覆盖已删除的文本(想想在 数组).
删除文本或数据时文件不会自动缩小;它们保持相同的大小(将它们视为数组)。
1. Read all record from file one by one
2. if (read_record==record_to be deleted)
skip
3. else
write record into new temporary file
4. Repeat step from 2 to 3 till all file is read
5. now delete original file
6. rename temporary file to original name