CJCoding With Joseph
15free / daySign inCreate account

Write a function int writeAndSum(const string& filename, const vector<int>& v) that uses ONE fstream object for both directions: 1. Open filename in ios::in | ios::out | ios::trunc mode. 2. Write every number in v to the file separated by single spaces, followed by a newline. 3. Move the read position back to the start with seekg(0). 4. Read the numbers back from the file with >> until reading fails, adding them up as you go. 5. Return the sum you read from the file.

The test prints the returned sum on its own line. For v = {1, 2, 3, 4, 5} it prints the output shown in Expected Output.

Do NOT write a main function.

Expected output

15
12

= newline. The output ends with a newline, so 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.