I am using a random access file in which i want to delete a line which satisfies some condition like if i have records
我正在使用随机访问文件,其中我想删除一条满足某些条件的行,就像我有记录一样
MCA 30
MBA 20
BCA 10
now my requirement is if i enter MBA then second line is to be deleted.
现在我的要求是,如果我输入MBA,那么第二行将被删除。
3 个解决方案
#1
2
Generally, removing an item from the middle of the file means rewriting all entries after the entry so as to use the space the item occupied.
通常,从文件中间删除项目意味着在条目之后重写所有条目,以便使用项目占用的空间。
Some things instead mark items that are deleted with some invalid value so as to spot that the slot is unused. Typically they don't even reuse deleted slots, as this is rather more management than you might imagine, and is basically implementing a heap-like architecture in a file. They need a separate 'compacting' step to remove this dead space later. Microsoft Jet (as in Access) worked like this.
有些事情会标记使用某些无效值删除的项目,以便发现该插槽未使用。通常,他们甚至不会重复使用已删除的插槽,因为这比您想象的更加管理,并且基本上是在文件中实现类似堆的体系结构。他们需要一个单独的“压缩”步骤来消除此死空间。 Microsoft Jet(在Access中)就像这样工作。
There's a very cool optimisation that's applicable in some cases:
在某些情况下,有一个非常酷的优化适用:
If the rows are unordered, and equal length, you can overwrite the entry you want to 'delete' with the last entry, and truncate the file.
如果行是无序的,并且长度相等,则可以使用最后一个条目覆盖要“删除”的条目,并截断该文件。
If the rows are unordered but not fixed length, you can use a more complicated variant of this approach where you move some entry from near the end that is the same length as the item being removed, so as only to need to shuffle up as few entries as possible.
如果行是无序的但不是固定的长度,你可以使用这种方法的一个更复杂的变体,你可以从一个与被删除的项目相同的长度移动一些条目,这样只需要将它们移动到很少的位置。条目尽可能。
#2
1
If it is a simple text file than you'll have to copy everything except the MBA line(s) to a new file. Random Access files don't really support delete or insert.
如果它是一个简单的文本文件,则必须将除MBA行之外的所有内容复制到新文件中。随机访问文件实际上不支持删除或插入。
Optimization: move everything after the MBA line up (in the same file)
优化:在MBA排队后移动所有内容(在同一文件中)
Alternative: use something more structured like a database.
替代方案:使用更像结构化的数据库。
#3
#1
2
Generally, removing an item from the middle of the file means rewriting all entries after the entry so as to use the space the item occupied.
通常,从文件中间删除项目意味着在条目之后重写所有条目,以便使用项目占用的空间。
Some things instead mark items that are deleted with some invalid value so as to spot that the slot is unused. Typically they don't even reuse deleted slots, as this is rather more management than you might imagine, and is basically implementing a heap-like architecture in a file. They need a separate 'compacting' step to remove this dead space later. Microsoft Jet (as in Access) worked like this.
有些事情会标记使用某些无效值删除的项目,以便发现该插槽未使用。通常,他们甚至不会重复使用已删除的插槽,因为这比您想象的更加管理,并且基本上是在文件中实现类似堆的体系结构。他们需要一个单独的“压缩”步骤来消除此死空间。 Microsoft Jet(在Access中)就像这样工作。
There's a very cool optimisation that's applicable in some cases:
在某些情况下,有一个非常酷的优化适用:
If the rows are unordered, and equal length, you can overwrite the entry you want to 'delete' with the last entry, and truncate the file.
如果行是无序的,并且长度相等,则可以使用最后一个条目覆盖要“删除”的条目,并截断该文件。
If the rows are unordered but not fixed length, you can use a more complicated variant of this approach where you move some entry from near the end that is the same length as the item being removed, so as only to need to shuffle up as few entries as possible.
如果行是无序的但不是固定的长度,你可以使用这种方法的一个更复杂的变体,你可以从一个与被删除的项目相同的长度移动一些条目,这样只需要将它们移动到很少的位置。条目尽可能。
#2
1
If it is a simple text file than you'll have to copy everything except the MBA line(s) to a new file. Random Access files don't really support delete or insert.
如果它是一个简单的文本文件,则必须将除MBA行之外的所有内容复制到新文件中。随机访问文件实际上不支持删除或插入。
Optimization: move everything after the MBA line up (in the same file)
优化:在MBA排队后移动所有内容(在同一文件中)
Alternative: use something more structured like a database.
替代方案:使用更像结构化的数据库。