File I/O - Delete Record

M

Magix

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?
void DeleteRecord(int LineNum)
{
// open FileA
// open tmpFile
// copy all the contents in FileA except on LineNum to tmpFile
// re-copy tmpFile to FileA
}
 
D

dandelion

Magix said:
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.

<OT>
Google "Indexed Sequential Access Method" (ISAM).
void DeleteRecord(int LineNum)
{
// open FileA
// open tmpFile
// copy all the contents in FileA except on LineNum to tmpFile
// re-copy tmpFile to FileA
}

That would give you rather a lot of overhead when deleting a number of
lines/records.
 
L

Lawrence Kirby

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

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.

Lawrence
 
D

dandelion

Lawrence Kirby said:
It isn't unreasonable to consider this from a C perspecive i.e. given the
file handling tools that C provides.


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.
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.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top