34. Removing empty lines from a text file

A possible approach to solving this task is to do the following:

  1. Create a temporary file to contain only the text you want to retain from the original file
  2. Read line by line from the input file and copy to the temporary file all lines that are not empty
  3. Delete the original file after finishing processing it
  4. Move the temporary file to the path of the original file

An alternative is to move the temporary file and overwrite the original one. The following implementation follows the steps listed. The temporary file is created in the temporary directory returned by filesystem::temp_directory_path():

namespace fs = std::experimental::filesystem;void remove_empty_lines(fs::path filepath){ std::ifstream ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.