CJCoding With Joseph
15free / daySign inCreate account

Read the Nth Fixed Record

Write a function string nthRecord(const string& filename, int n) that opens filename with an fstream in ios::in | ios::binary mode, seeks the read position to byte n * 4, reads that one record with getline, and returns the three letters (without the newline).

Every record in the file is exactly 4 bytes: three uppercase letters followed by one newline. n is always a valid record index.

The test creates the file in binary mode, calls your function and prints the returned record on its own line. For a file with the lines AAA, BBB, CCC and nthRecord("records_a.txt", 1) it prints the output shown in Expected Output.

Do NOT write a main function.

Expected Output:

BBB
FOG
RED

= 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.