removing records from a data file

To start out, I am learning C++ in an online class environment. This can make it hard to get prompt answers to question from instructors sometimes.

My question(s) is/are this:

I am trying to modify a program that, is supposed, to prompt you for an personel number, name, city, year, and salary. Once this is entered it places it in a file, also chosen by the user.

The program I am modifing came from a text website, so not sure how correct it was to start with. I then have to take this info and modify, find and the modification portion of the program is to add an option to delete a record. This is explained to define a function to writeNullToFile to overwrite the record to be deleted by a null record. This is explained as a nonnumeric (tombstone) in the first portion of the Personel number.

This will then be used to overwrite the record we search for and overwrite with the null record. Then the database destructor should be invoked, which will copy non-null records to a new data file, deletes the old one and renames the new data file with the name of the old data file.

We are given the .cpp files as well as the .h files for this project and have to modify them to work for this project. I have worked on this thing for quite a while and have gotten no where. I am confused on the tombstone character as well as the writeToNull function. We are also supposed to create a function called isNull() to check to see if a record is null, not even sure where this would be used. the modifications will be mostly on the personal and student cpp/h files. (I think)

I thank anyone in advance for helping out, I am seriously drowning.

Mike

Comments

: deletes the old one and renames the new data file with the name of : the old data file.: : We are given the .cpp files as well as the .h files for this project : and have to modify them to work for this project. I have worked on : this thing for quite a while and have gotten no where. I am confused : on the tombstone character as well as the writeToNull function. We : are also supposed to create a function called isNull() to check to : see if a record is null, not even sure where this would be used. the : modifications will be mostly on the personal and student cpp/h : files. (I think) : : I thank anyone in advance for helping out, I am seriously drowning.: : Mike

>>I am confused on the tombstone character You can choose any character you want that will not be a normal part of the key field. For example I often use '~' because that's very unlikely to be a valid key field character. Then to delete a record just overwrite the first byte with the tombstone character. When reading the file back into memory ignore records that have been marked for deletion.

>>are also supposed to create a function called isNull() to check to see if a record is nullJust create a function with a parameter pointer to current record in memory. Return TRUE if the first character of the key field is the tombstone character or FALSE if not.[code]int isNull(const struct* record){ return (record->key_field[0] == tombstone_character) ? TRUE : FALSE;}[/coodewhere tombstone_character has been previously #define'ed

=============================================never lie -- the government doesn't like the competition. (Author unknown)

: : deletes the old one and renames the new data file with the name of : : the old data file.: : : : We are given the .cpp files as well as the .h files for this project : : and have to modify them to work for this project. I have worked on : : this thing for quite a while and have gotten no where. I am confused : : on the tombstone character as well as the writeToNull function. We : : are also supposed to create a function called isNull() to check to : : see if a record is null, not even sure where this would be used. the : : modifications will be mostly on the personal and student cpp/h : : files. (I think) : : : : I thank anyone in advance for helping out, I am seriously drowning.: : : : Mike: : >>I am confused on the tombstone character : You can choose any character you want that will not be a normal part : of the key field. For example I often use '~' because that's very : unlikely to be a valid key field character. Then to delete a record : just overwrite the first byte with the tombstone character. When : reading the file back into memory ignore records that have been : marked for deletion. : : >>are also supposed to create a function called isNull() to check to : see if a record is null: Just create a function with a parameter pointer to current record in : memory. Return TRUE if the first character of the key field is the : tombstone character or FALSE if not.: [code]: : int isNull(const struct* record): {: return (record->key_field[0] == tombstone_character) ? TRUE : FALSE;: }: [/coode: where tombstone_character has been previously #define'ed: : : : : =============================================: never lie -- the government doesn't like the competition. (Author unknown)

Thanks for that info, now my next question is this. Do you just use a substring to pull the characters and overwrite from (0,0)? Also when I overwrite, how do I approach the deletion? I understand that we will have a line that will start with tombstone character, marking it as a file to delete. Do I just write a new file with all lines not starting with the tombstone character then rename it with the new file name? That sounds like the direction I need to be looking, so, without sounding like I am totally stupid, how do I approach that? This has been a thorn in my side for about 2 months. I had to drop a class for this program. I am hoping once I see what is happening the "lightbulb" will go on. Thanks for the help.

Yes, that's the idea, assuming the first character on the line is also the first character of the key field. For example assume the key field is the student ID number and that number is somewhere in the middle of the line. Then you have to overwrite the first character of the student ID number with the tombstone character. Of course to make life a lot easier for you, you should make the first field in each line the student ID number, but that's not absolutely essential.: >>I had to drop a class for this programYou should have posted this question over 2 months ago so that someone could have helped you. Its really not a difficult assignment, but does require some pre-planning of the file record layout.=============================================never lie -- the government doesn't like the competition. (Author unknown)

: Yes, that's the idea, assuming the first character on the line is : also the : first character of the key field. For example assume the key field : is the : student ID number and that number is somewhere in the middle of the : line. : Then you have to overwrite the first character of the student ID : number : with the tombstone character. Of course to make life a lot easier : for : you, you should make the first field in each line the student ID : number, but that's not absolutely essential.: : : >>I had to drop a class for this program: You should have posted this question over 2 months ago so that : someone : could have helped you. Its really not a difficult assignment, but : does require some pre-planning of the file record layout.: =============================================: never lie -- the government doesn't like the competition. (Author : unknown)

Yea, I know I should have posted earlier, but I had just started a new job and was having to get settled in there as well. The first field is the Student ID number, so that will help. Any tips on deleting/renaming the data file? I do understand that basically I would write the old file with the null values then create the new(temp) file writing only the values that weren't considered null, then rename the temp file with the name of the old file name. What would be the best way to tackle this scenario? I certainly appreciate all the input so far. Hopefully I can get this thing put together so I can say I wasn't beaten by this thing.