Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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..
Posted
Comments
Philippe Mori 5-Sep-11 9:38am    
What do you store in your binary file? How big is the file? How big is one record? How many record do you have? How do you use the file (sequential reading or queries...)? Are the records sorted? How the file is used (one record at a time, processed sequentially, fully loaded in memory)?

As indicated in my solution, a database might be a better fit than a binary file.
Philippe Mori 5-Sep-11 9:39am    
By the way, there are no standard way of handling binary files for such operations...

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Sep-11 0:24am    
Good explanation, my 5.
--SA
Sergey Alexandrovich Kryukov 5-Sep-11 0:30am    
I added some explanation in my solution. I think it could be helpful -- please see.
--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
 
Share this answer
 
Comments
Philippe Mori 5-Sep-11 9:50am    
My 5. Good answer... Although, I suggest the OP to consider using a database instead.
Sergey Alexandrovich Kryukov 5-Sep-11 11:04am    
Thank you. Philippe.
I up-voted your reasonable suggestions.
--SA
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).
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Sep-11 11:03am    
A reasonable suggestions to look for alternative. For XML option, I would also suggest to use Data Contract, it will automatically store and restore any object graph without any effort from the user and no need to use XML or JSON directly. My 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900