← Back to Streams
Question 613
Aligned Table to File
Aligned Table to File
Write a functionvoid writeTable(const string& filename, const vector<pair<string, int>>& rows)that opensfilenamewith anofstreamand writes one line per row: the name left-aligned in a field 8 characters wide (left,setw(8)), immediately followed by the quantity right-aligned in a field 4 characters wide (right,setw(4)), then a newline. There is no separator between the two fields other than the padding spaces. Names are always shorter than 8 characters and quantities always fit in 4. For rows {{"apple", 5}, {"pear", 12}} the file must contain: apple 5 pear 12 The test reopens the file and prints every line. Do NOT write a main function.
Expected Output:
apple 5 pear 12 kiwi 100 banana 3
= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
Streamsofstream
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.