CJCoding With Joseph
15free / daySign inCreate account

Fixed-Width Record Editor

Build a class RecordFile that 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, so bob is bob plus five spaces; record i starts at byte i * 8. Open the file with ios::binary in every method.

Implement:
- RecordFile(const string& filename): stores the filename and creates the file if it does not exist (opening it once in ios::out | ios::app | ios::binary is enough).
- void append(const string& name): opens in ios::out | ios::app | ios::binary and writes the name as left << setw(8), so exactly 8 bytes are added.
- string get(int i) const: opens in ios::in | ios::binary, seeks to byte i * 8, reads 8 characters with read, and returns the name with trailing spaces removed.
- void update(int i, const string& name): opens in ios::in | ios::out | ios::binary, moves the write position to byte i * 8 with seekp, 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.

i is always valid. The test starts with an empty file and prints each result on its own line. For append("alice"), append("bob"), then get(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
Topics:
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.