← Back to Streams
Question 667
Fixed-Width Record Editor
Fixed-Width Record Editor
Build a classRecordFilethat stores names in a binary file as fixed-width records of exactly 8 bytes, no separators. A name (1 to 8 characters, no spaces) is padded on the right with spaces to 8 bytes, sobobisbobplus five spaces; recordistarts at bytei * 8. Open the file withios::binaryin every method. Implement: -RecordFile(const string& filename): stores the filename and creates the file if it does not exist (opening it once inios::out | ios::app | ios::binaryis enough). -void append(const string& name): opens inios::out | ios::app | ios::binaryand writes the name asleft << setw(8), so exactly 8 bytes are added. -string get(int i) const: opens inios::in | ios::binary, seeks to bytei * 8, reads 8 characters withread, and returns the name with trailing spaces removed. -void update(int i, const string& name): opens inios::in | ios::out | ios::binary, moves the write position to bytei * 8withseekp, and overwrites that record with the padded name. No other record changes. -int count() const: the number of records — the file size divided by 8.iis always valid. The test starts with an empty file and prints each result on its own line. Forappend("alice"),append("bob"), thenget(1),count(),update(0, "zed"),get(0)it prints the output shown in Expected Output. Do NOT write a main function.
Expected Output:
bob 2 zed 1 x longname 2 kim
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Streamsfstream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.