请求从二进制文件中删除特定记录的代码请求 [英] Sorce code request for deletion of a particular record from binary file

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

问题描述

我必须做一个有关C ++中网吧管理的小型项目.因此,我需要源代码从二进制文件中删除特定记录.我使用fstream类对象对其进行操作.请为我提供一个代码..

I have to do a mini project on cybercafe management in C++. Hence I need the source code to delete a particular record from the binary file. I use fstream class objects for its manipulation. Please suggest me a code for it..

推荐答案

除非您计划保留冗余信息,否则,方法是重写二进制文件,而只需省略您要删除的记录.

在不了解文件结构或文件记录位置的方法的情况下,要提出一个好的解决方案是相当困难的.

如果文件的开头具有目录"类型结构,则可以简单地删除要删除的记录的目录条目-尽管实际上并不会删除数据,而是删除了在目录"中将其作为条目-或者换句话说,删除将用于在文件中定位记录的数据.
Unless you plan on keeping redundant information, the way to do it is to re-write the binary file, simply ommitting the record you''d like to delete.

It''s rather difficult to suggest a good solution with no knowledge of the structure of the file or the method by which records in the file are located.

If the file has a ''directory'' type structure at the start of it, then you could simply remove the directory entry for the record you''d like to delete - though this will not actually remove the data - but rather remove the entry for it in the ''table of contents'', as it were - or to put it another way, remove the data which would be used to locate the record within the file.


只是为enhzflep解决方案添加说明.

您要问的是不适定"的问题.开发二进制文件系统并可能基于现有的二进制文件格式从文件中删除单独的记录是没有意义的.在制定时,通常情况下,解决问题看起来并不可行.在许多特殊情况下(一个简单的例子:长度完全相同的记录序列)可能是完全可行的,但是我们没有此信息.

相反,您可以从头开始设计文件格式,从一开始就要求随机删除单独的记录. enhzflep提到的目录"类型只是这种设计的一个例子.

—SA
Just to add explanation to the solution by enhzflep.

What you ask is the "ill-posed" problem. Developing a binary file system with the possibility to remove separate records from a file based on existing binary file format makes no sense. As it is formulated, in general case, the resolution of a problem does not look feasible. It could be quite feasible in a number of special cases (simple example: a sequence of records of the exact same length), but we don''t have this information.

Rather, you could design the file format from scratch taking the requirements of random deletion of separate records from the very beginning. The ''directory'' type mentioned by enhzflep is just one example of such design.

—SA


如果可能的话,我建议您将二进制文件替换为数据库或文本文件(XML),因为这样会更容易处理这些文件文件.

另一种可能性,取决于您的实际文件格式,可能只是有一个已删除"标记,然后在读取文件时忽略了这些空洞".如有必要,然后可以压缩文件.

在实践中,如果您使用固定长度的记录或能够压缩数据,则二进制文件通常对性能有好处,但是在以后的版本中它们很难操作或更新格式.

考虑到您的项目(网络咖啡管理),我绝对建议使用数据库,因为对于此类应用程序,数据库通常是最佳选择.

添加字段,删除记录和增强数据库功能(例如在保存数据时发生崩溃的情况下)要容易得多.

对于通常受大小限制(几千字节)的设置或项目文件,XML通常是首选.
If possible, I would suggest you to replace your binary file with a database or a text file (XML) as it would much easier to manipulate those files.

Another possibility, depending on your actual file format, might to just have a "deleted" marker and then ignore those "holes" when reading the file. If necessary, then you might compact the file afterwards.

In pratice, binary files are generally good for performance if you uses fixed length record or are able to compress the data but they are harder to manipulate or update the format in subsequent revisions.

Given your project (cybercafe managment), I would definitively suggest to use a database as it is generally the best choice for such applications.

It is much easier to add fields, delete record and to be robust with a database (in the case of a crash while saving the data for example).

XML is generally the prefered choice for settings or project files that are generally limited in size (a few kilobytes).


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

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