← Back to Streams
Question 575
Read One Integer from a File
Read One Integer from a File
A text file starts with a whole number, for example: 42 Write a functionint readNumber(const string& filename)that returns the first integer stored in the file. The starter already opens the file asifstream inand declaresint n = 0; add the one line that reads an integer frominintonwith>>. Only the first number matters even if the file contains more, and it may be negative. The test prints the returned value on its own line, for example (see Expected Output). Do NOT write a main function.
Expected Output:
42 -7
= newline. The output ends with a newline — print one after the last line.
Hints2 available
Sign in to use hintsTopics:
Streamsifstream
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.