Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating a binary file

Status
Not open for further replies.

DerPflug

Programmer
Mar 28, 2002
153
US
Can anyone offer any practical steps in updating and deleting records in a binary file? I'm writing a simple app (console application) that updates, deletes, and inserts records in a binary inventory file. All of the records are entered as structs.
My main conundrum is what the series of events is in each of the functions.

update: How can I allow the user to pull up the requested file and be able to edit it while in the window, meaning jump from field to field and then save it?

delete: Deletes are accomplished with a logical delete, i.e. a delete flag.

Thanks for any insight.
 
If the records aren't sorted, you could iteratively search through the file, record by record, until you find the proper record to update.

while (fread(&foo,sizeof foo,1,fp)==1) {
/* Compare foo to the search
* record. Exit loop if it matches. */
}

Once you've found it, you'll need to fseek() back one record (since the file pointer will have been advanced to the next record)

fseek(fp,-(sizeof foo),SEEK_CUR);

And then use fwrite() to overwrite the record with the updated one.

fwrite(&new_rec,sizeof new_rec,1,fp);

To delete a record, you would use the same search function, except you would write a "dummy" record in the appropriate place in the file with the delete flag set.

If you've sorted the records in the file, you could do a binary search with fseek().
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top