Delete a record by reading structure information from a binary file into an array and back into the file C++

StackOverflow https://stackoverflow.com/questions/22213158

سؤال

Okay guys, first off, yes this is homework, but I do not have the required ranking to place that tag in the tag section, so I am letting you know here. Also, I am fairly new to C++, so please keep that in mind with your responses, I am trying to keep my brain from exploding already.

I have created a Customer structure that allows the user to enter a new customer record, alter an existing customer record, display a specific customer record, display all records, and delete a specific customer record, and everything must be done as a binary file. The part I am having problems with is deleting a specific customer record, the best solution I could come up with was to write all of the records except the one to be deleted into an array, and then re-write the array back into the binary file.

Well, that is what I attempted to do, and it will compile, but when I go to run the program it freezes at the part where you delete a customer record. My code is crazy-long (1114 lines), so I am just going to post the section I am having problems with.

If you guys can tell me what I am doing wrong or show me a better way to delete a record, I would very, very much appreciate it. Please -try- to keep it on the C++ for dummies level (which is what I feel like).

هل كانت مفيدة؟

المحلول

Now you read all the records (upto 150): Customer custRecords[150]; but save only one deleteCustomer.write(reinterpret_cast<char *>(custRecords), sizeof(newCustomer));. Look at the end of the call , sizeof(newCustomer) - it is only one record, you should multiply it by number of records that you have read into custRecords before.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top