"Lawrence Kirby" <lk****@netactive.co.uk> wrote in message
news:pa****************************@netactive.co.u k...
On Wed, 12 Jan 2005 12:01:18 +0100, dandelion wrote:
"Magix" <ma***@asia.com> wrote in message
news:41**********@news.tm.net.my... Hi,
This is about File I/O Operation.
I want to delete a record from a file, let say FileA. Any one has any
good and efficient example/approach?
Hmmm... I'm quite sure this was a newsgroup on 'C' just a minute ago. So
unless you got a C-related problem with your implementation, the post is
off-topic.
It isn't unreasonable to consider this from a C perspecive i.e. given the
file handling tools that C provides.
<OT>
Google "Indexed Sequential Access Method" (ISAM). </OT>
That's one alternative. It does depend on the file in question being ISAM
compatible, and you having an ISAM library available
It also presents a not-too-difficult mechanism to "save up" deletions and
handle them in one go. This dramatically improving response times.
void DeleteRecord(int LineNum)
{
// open FileA
// open tmpFile
// copy all the contents in FileA except on LineNum to tmpFile //
re-copy tmpFile to FileA
This is a possible approach, and reasonable for small files. I'd just
observe here that standard C defines a rename() function which could be
used for the last step.
}
}
That would give you rather a lot of overhead when deleting a number of
lines/records.
You can still delete a number of lines/records in one pass, just avoid
copying all of the lines in question.
Yes. But that is not in the algorithm as presented.
Not too difficult to implement, though. Downside is, you have to have a
"list" of records to delete. If the program is driven by a UI, this will
most likely not be the case and response times will be dramatic.
Anyway. I think the OP would be alot better off using a
(commercial/opensource/whatever) standard solution than trying to reinvent
the wheel.