March 14th, 2013, 03:30 PM
Database that stores employee information (using structures)
Hey guys, I've been given an assignment by one of my lecturers and I think it's a bit out of the class's reach at the moment.
Basically we've got to create a database for a hypothetical company using structures, and the user should be able to add employee information, delete information, list the employees in the database and compact the data (reordering the list, removing the deleted employees)
Obviously I don't want someone to do the work for me, but I could use some advice on how to delete an employee's information and how to compact the data.
She said that once the user selects an employee to delete, the first character of the employee's name should be replaced with a '*', and I've got an idea on what to do (as in get a function to search for a * and replace all data after that up to certain character I designate) but I have no idea on how to compact the information.
Any help or links to places that could help me out would be great, thanks in advance!
March 14th, 2013, 03:43 PM
Basically when you compact the database you actually remove the deleted entries. When you delete the record you just need to replace the first character with your *, it doesn't sound like you need to do anything else to that record.
March 14th, 2013, 03:53 PM
This sounds like a job for a linked list (or maybe even doubly linked). Removing deleted employees from a linked list is fast and easy.
I would assume that reordering means things like sorting by name, ID number, etc.
March 14th, 2013, 05:04 PM
Basically, she's having you use the approach that dBaseII and dBaseIII used: "delete" a record by marking it as deleted in which case the program will just ignore that record. Then pack the database file explicitly to remove the deleted records. Especially on the earlier generation PCs the PACK action would take much longer than several deletes would, so postponing the PACK until later made dBase run more efficiently; this was most especially true when the database file was on a floppy.
BTW, that is also how MS-DOS would delete a file from a directory: it would mark the file as being deleted by changing the first character of the file name and then return its sectors to the disk's "free list". This approach has been described as being like removing a book from a library simply by removing its card from the card catalogue. This is also how Norton Utilities used to be able to restore deleted files.
A basic procedure for removing records from a file would be this:
1. Open the file in read mode and open a temporary file in write mode.
2. Read each record from the database file and write it to the temp file only if the record is not marked as being deleted.
3. When you reach the end of the database file, then close both files. At this point, the temp file contains all the records that have not been deleted, which means that the temp file is a modified copy of the original database file.
4. Delete the database file.
5. Rename the temp file to the same name as the database file. You have now replaced the database file with a modified copy of itself and so effectively have "modified" the original file.
You will use this general approach many times in the future. It's so common that MS-DOS even had a special system call (INT 21H Function 5AH) to create a temporary file with a unique name.