By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

File I/O - Delete Record

P: n/a
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
}
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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

<OT>
Google "Indexed Sequential Access Method" (ISAM).
</OT>
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.
Nov 14 '05 #2

P: n/a
"Magix" <ma***@asia.com> wrote:
I want to delete a record from a file, let say FileA. Any one has any good
and efficient example/approach?


<http://www.eskimo.com/~scs/C-faq/q19.14.html>

Richard
Nov 14 '05 #3

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:41**************@news.individual.net...
"Magix" <ma***@asia.com> wrote:
I want to delete a record from a file, let say FileA. Any one has any good and efficient example/approach?
<http://www.eskimo.com/~scs/C-faq/q19.14.html>


Wow...

Missed that one.
Richard

Nov 14 '05 #4

P: n/a
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
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.

Lawrence
Nov 14 '05 #5

P: n/a

"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.
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.