从文件中删除记录? [英] Delete a record from a file?

查看:123
本文介绍了从文件中删除记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个文本文件来存储我的数据记录。数据以以下格式存储。

 Antony | 9876543210 
Azar | 9753186420
Branda | 1234567890
David | 1357924680
John | 6767676767

成千上万的记录存储在该文件中。我想删除一个特定的记录,说大卫| 1357924680。我正在使用C,如何有效地删除特定的记录?目前我正在使用临时文件通过省略我想要删除的记录将记录复制到该临时文件。并复制到临时文件后,我通过截断原始文件的所有内容将临时文件的内容复制到原始文件。我不认为我正在有效地做到这一点。帮助我。

解决方案

在您的数据中添加一列,指示其是有效的(1)还是删除的(0)

  Antony | 9876543210 | 1 
Azar | 9753186420 | 1
Branda | 1234567890 | 1
David | 1357924680 | 1
John | 6767676767 | 1

当您想要删除记录,覆盖单个字节:

  Antony | 9876543210 | 1 
Azar | 9753186420 | 1
Branda | 1234567890 | 0
David | 1357924680 | 1
John | 6767676767 | 1

Branda现在被删除。

然后添加一个数据文件压缩功能,它可以用来重写不包含已删除行的文件。这可以在低或没有使用的时候完成,所以它不会干扰正常的操作。



编辑



有效性列应该是第一列,因此您可以更轻松地跳过删除的行。


i am using a text file to store my data records. the data is stored in the following format.

Antony|9876543210
Azar|9753186420
Branda|1234567890
David|1357924680
John|6767676767

Thousands of records are stored in that file. i want to delete a particular record, say "David|1357924680". I am using C, how to delete the particular record efficiently? currently i am using a temporary file to copy the records to that temp file by omitting the record i want to delete. and after copying to temp file, i copy the contents of the temp file to original file by truncating all the contents of the original file. i don't think that i am doing it efficiently. Help me.

解决方案

Add a column to your data indicating it is either a valid ( 1 ) or deleted ( 0 ) row:

Antony|9876543210|1
Azar|9753186420|1
Branda|1234567890|1
David|1357924680|1
John|6767676767|1

When you want to delete a record, overwrite the single byte:

Antony|9876543210|1
Azar|9753186420|1
Branda|1234567890|0
David|1357924680|1
John|6767676767|1

Branda is now deleted.

Then add a data file compression function which can be used to rewrite the file excluding deleted rows. This could be done during times of low or no usage so it doesn't interfere with regular operations.

Edit

The validity column should probably be the first column so you can skip deleted rows more easily.

这篇关于从文件中删除记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆