Strip Blank Lines
mediumQuestion 664Write a function int stripBlankLines(const string& filename) that removes every blank line from a text file and returns how many lines were removed. A blank line is a line with no characters at all.
Read all lines with getline, keep the non-blank ones in a vector<string>, then reopen the file in ios::out | ios::trunc mode and write the kept lines back, each followed by a newline.
The test prints the returned count on its own line and then every line of the file. For a file whose four lines are a, an empty line, b, and another empty line it prints the output shown in Expected Output.
Do NOT write a main function.
Expected output
2 a b 0 x y
= newline. The output ends with a newline, so print one after the last line.
Hints4 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.