{"id":3914,"date":"2020-01-01T00:01:12","date_gmt":"2020-01-01T08:01:12","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3914"},"modified":"2020-01-09T10:32:28","modified_gmt":"2020-01-09T18:32:28","slug":"remove-trailing-blank-lines","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3914","title":{"rendered":"Remove Trailing Blank Lines"},"content":{"rendered":"<p>Recently, I wrote a utility that required the final line of text in a file to terminate with a special code. The code had to sit at the end of a line of text, not on a blank line. What I discovered is that many text files end with one or more blank lines.<br \/>\n<!--more--><br \/>\nThe blank lines, essentially one newline character following another, are a valid part of any text file. In fact, I recall from decades ago online debates over whether text files (source code or even email messages) should end with an extra blank line &#8220;just in case.&#8221;<\/p>\n<p>A text file is a text file. It need not end with a newline character at all. Yet, as I was writing my utility, I discovered that some text files didn&#8217;t end with a newline, some ended with a single newline, many with two (the last paragraph followed by a blank line), and a handful with multiple blank lines.<\/p>\n<p>The task for this month&#8217;s Exercise is to write code that processes a text file, outputting all its contents minus any trailing blank lines. A final newline character is okay. No terminating newline is okay. But when the file tails with two or more newlines, they must not appear in the output.<\/p>\n<p>To test the code, I&#8217;m supplying four sample files:<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/12\/getty-0.txt\">getty-0.txt<\/a><br \/>\n<a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/12\/getty-1.txt\">getty-1.txt<\/a><br \/>\n<a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/12\/getty-3.txt\">getty-3.txt<\/a><br \/>\n<a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/12\/getty-mix.txt\">getty-mix.txt<\/a><\/p>\n<p>Each of these files terminates with a different number of newlines: zero, one, and three for the first three. The file <code>getty-mix.txt<\/code> has multiple blank lines internally as well as at the end, just to test your code.<\/p>\n<p>For your solution to work properly, the output of the first three files looks like this:<\/p>\n<p><code>Four score and seven years ago our fathers brought<br \/>\nforth on this continent a new nation, conceived in<br \/>\nliberty, and dedicated to the proposition that all<br \/>\nmen are created equal.<\/code><\/p>\n<p>For the <code>getty-mix.txt<\/code> file, output looks like this:<\/p>\n<p><code>Four score and seven years ago our fathers brought<br \/>\nforth on this continent a new nation, conceived in<\/p>\n<p>liberty, and dedicated to the proposition that all<\/p>\n<p>men are created equal.<\/code><\/p>\n<p>No blank line follows the last line in each example.<\/p>\n<p>I can think of several solutions for this Exercise. In fact, my own solution is different than the one I used for the utility I wrote that spawned this idea. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3929\">Click here<\/a> to view my solution. Before then, please try this Exercise on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most text files end with a blank line.  One is enough! When a text file terminates with several blank lines, you can write a utility to remove the excess. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3914\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3914","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3914"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3914\/revisions"}],"predecessor-version":[{"id":3954,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3914\/revisions\/3954"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}