← Back to Streams
Question 660
Read the Nth Fixed Record
Read the Nth Fixed Record
Write a functionstring nthRecord(const string& filename, int n)that opensfilenamewith anfstreaminios::in | ios::binarymode, seeks the read position to byten * 4, reads that one record withgetline, and returns the three letters (without the newline). Every record in the file is exactly 4 bytes: three uppercase letters followed by one newline.nis 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 linesAAA,BBB,CCCandnthRecord("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
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.