{"id":6467,"date":"2024-07-06T00:01:16","date_gmt":"2024-07-06T07:01:16","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6467"},"modified":"2024-06-29T10:25:25","modified_gmt":"2024-06-29T17:25:25","slug":"erasing-text-for-stream-output","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6467","title":{"rendered":"Erasing Text for Stream Output"},"content":{"rendered":"<p>One of my first programming obsessions was online communications. I wrote several modem programs for the TRS-80 (in Z80 Assembly) and then moved to the IBM PC\/MS-DOS where I coded communications programs in both Assembly and C. I learned a few things.<br \/>\n<!--more--><br \/>\nOne of the surprising things I learned was how to erase text on the screen. This process involves moving the cursor, which works differently online than it does on the terminal screen.<\/p>\n<p>When programming a single-user, single-tasking system like MS-DOS, a programmer has direct control over the computer&#8217;s hardware. Back then, I knew nothing of stream input and output as it&#8217;s used in C programming. But when I wrote online programs, I had to tussle with stream I\/O which works differently from manipulating the screen directly.<\/p>\n<p>For example, on the computer I could send a newline (<code>\\n<\/code>) to the screen and the cursor sped back to the start of the line and dropped down a line. If the cursor is at the bottom row of the screen, the lines above scroll upwards and a new blank line is inserted. All this action must be manually coded when working online: A newline output involves both a carriage return <em>and<\/em> a linefeed.<\/p>\n<p>Backing up and erasing is another issue. To backup the cursor, you send control code 8 (^H). This code moves the cursor back a notch, but it doesn&#8217;t erase. Backup is non-destructive. Yet most programs implement a destructive backup.<\/p>\n<p>The following code outputs a line of 20 Xs. The program pauses. Then code outputs 20 backspace characters.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_07_06-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_07_06-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=0;x&lt;20;x++)\r\n        putchar('X');\r\n    fflush(stdout);\r\n    sleep(1);\r\n    for(x=0;x&lt;20;x++)\r\n        putchar('\\b');\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The code outputs 20 Xs. The output buffer is flushed, <code>fflush(stdout)<\/code>, to make the line appear. Otherwise, text isn&#8217;t output until a newline is sent.<\/p>\n<p>The <code>unistd.h<\/code> header file is required for the <em>sleep()<\/em> function. This function is defined by the POSIX standard; it&#8217;s not part of the standard C library. It pauses one second for you to behold the line of Xs. Then 20 backspace characters are output. If you&#8217;re quick, you can see the cursor scurry back to the start of the line &mdash; but nothing is erased!<\/p>\n<p>Sample output:<\/p>\n<p><code>XXXXXXXXXXXXXXXXXXXX<\/code><\/p>\n<p>The reason the line isn&#8217;t erased is because backspace is non-destructive. The ^H character merely moves the cursor back one character position. To make it destructive, as I learned when programming online communications, you must output three characters: backspace, space, backspace.<\/p>\n<p>In the days of 300 baud modems, you could see the cursor &#8220;wiggle&#8221; as it backed up and erased text on the screen. Today the action happens too quickly, as you can see by this update to the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_07_06-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_07_06-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=0;x&lt;20;x++)\r\n        putchar('X');\r\n    fflush(stdout);\r\n    sleep(1);\r\n    for(x=0;x&lt;20;x++)\r\n        printf(\"\\b \\b\");\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>printf()<\/em> statement outputs a string backspace, space, backspace. The action takes place quickly in the terminal window, which is set to 38,400 baud on my system &mdash; 128 times faster than that old 300 baud modem. Regardless of speed, the effect is to backup and erase the line of input.<\/p>\n<p>For the output, the row of Xs appears, the program pauses, then the line is erased. This weird method worked online years ago, and it still works today.<\/p>\n<p>For next week&#8217;s Lesson, I cover another way to accomplish the same thing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Backing up is one thing. Backing up and erasing is three things. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6467\">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":[2],"tags":[],"class_list":["post-6467","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6467","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=6467"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6467\/revisions"}],"predecessor-version":[{"id":6483,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6467\/revisions\/6483"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}