← Back to Streams
Question 647
fstream in Read Mode
fstream in Read Mode
Write a functionstring readWithFstream(const string& filename)that opensfilenamewith anfstreaminios::inmode, reads the first line withgetline, and returns it (without the newline). The test creates the file, calls your function and prints the returned string on its own line. For a file whose lines arefirst lineandsecond lineit prints the output shown in Expected Output. Do NOT write a main function.
Expected Output:
first line alpha beta gamma
= newline. The output ends with a newline — print one after the last line.
Hints2 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.