← Back to Streams
Question 574
Read the First Word
Read the First Word
A text file contains words separated by spaces and newlines, for example: hello world Write a functionstring firstWord(const string& filename)that returns the first word of the file. The starter already opens the file asifstream inand declares a stringw; you only need to add the one line that reads a word frominintowwith>>. The test prints the returned word on its own line, for example (see Expected Output). Do NOT write a main function.
Expected Output:
hello apple
= 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.