June 24th, 2013, 10:53 AM
C++ how to modify record in file
I have a file account.dat within that data written in the format:
,where the first line indicates the number of the user's account, and the last current balance on the account.
I know how to read a line from a file and to write records in a file, but there should be some help on updating records, specifically, I want that if a user enters a new sum of money to update the file, that is to be updated last line of file.
Any help is appreciated.
June 24th, 2013, 11:09 AM
You don't normally "update" a text file, you "re-create" the file with the modified information.
Normally you read the information before the changes and write this information to a "temporary" file. Add the "updated" data, then continue reading the old information and write it to the "temporary" file as well. After you are finished, close then delete the old file and then rename the "temporary" file to the original file name.
June 24th, 2013, 11:13 AM
Tnx Jim, Do you know a forum where they described a similar 'operations' ? :)
June 24th, 2013, 11:21 AM
I assume that account.dat is a text file, meaning that it's a concatenation of lines of text which are delimited by a new-line sequence (In Windows, carriage return-line feed, AKA "CRLF", AKA \x0d\x0a).
If it's a normal text file, then you have variable-length records. In that case, just changing the balance would only work if the new value is exactly the same length as the old, which would be an unreasonable assumption.
Now, if your file used fixed-length records, that would be different. You could define each field in each record to have a fixed length, a length great enough to hold the longest allowable value of each field. Then you could simply find the record of your choice, find the field you want to modify, and simply modify it. That is basically what database programs use.
However, it appears that you are using variable-length records, so you would need to use the standard procedure for modifying a text file:
1. Open the file for reading and open a second temporary file for writing; give the temporary file any name you want, just so long as it's unique.
2. Search for the record to be modified by reading each record from account.dat and testing whether it's the one you want. If it's not, then write the record to the temporary file.
3. When you find the record you want to modify, read each field into variables, modify the variables, and then write those variables to the temporary file.
4. Write all the remaining records in account.dat into the temporary file using the same method you had used when searching for the record.
5. Close both files. Delete account.dat (or rename it as a back-up copy). Rename the temporary file as "account.dat".
Now, since you state that this is to be done in C++, I assume that your teacher wants you to use iostreams. My condolances.
Of course, if account.dat becomes too large, then having to copy the entire file for each transaction could become time-consuming. If the records in account.dat do not need to be sorted, then you could employ a trick that dBASE used. You could open the file for read/write. When you find the record that you want to modify, read the fields into variables and modify the variables as before, but then you mark the record as invalid and then you append the modified record at the end of the file. If you want, you could also write a function that you would run occasionally to remove invalid records from the file, much like dBASE's PACK command.
June 24th, 2013, 11:29 AM
Yes, I use variable-length records.
I'll stick to these 5 steps, because not that updated record only add to the end of the file :)
Thank you very much for your help!
June 24th, 2013, 12:53 PM
This 'algorithm' works, tnx!