← Back to Streams
Question 664
Strip Blank Lines
Strip Blank Lines
Write a functionint 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 withgetline, keep the non-blank ones in avector<string>, then reopen the file inios::out | ios::truncmode 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 area, 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 — 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.