{"id":3379,"date":"2018-12-01T00:01:33","date_gmt":"2018-12-01T08:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3379"},"modified":"2018-11-24T10:52:52","modified_gmt":"2018-11-24T18:52:52","slug":"the-ins-and-outs-of-fgets","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3379","title":{"rendered":"The Ins and Outs of <em>fgets()<\/em>"},"content":{"rendered":"<p>With the demise of the <em>gets()<\/em> function, <em>fgets()<\/em> remains the top C language text-input function. Whether reading from a file or from standard input, the function is quite useful, but it&#8217;s not without some quirks.<br \/>\n<!--more--><br \/>\nThe key to understanding how <em>fgets()<\/em> works, is to know how it reads characters. First, the format:<\/p>\n<p><code>char *fgets(char *buffer, int size, FILE *stream);<\/code><\/p>\n<p><code>buffer<\/code> is a <em>char<\/em> array or chunk of memory where the characters fetched are stored.<\/p>\n<p><code>size<\/code> is the size of the <code>buffer<\/code>, the same value. The <em>fgets()<\/em> function reads <strong>one less<\/strong> than the <code>size<\/code> value to ensure that the input string is capped with a null character, <code>\\0<\/code>. When a newline (<code>\\n<\/code>) is encountered before the <code>size<\/code> value is reached, input stops, the newline is retained, and the null character appended.<\/p>\n<p><code>stream<\/code> is the handle of an open file or, typically, the <code>stdin<\/code> constant representing standard input.<\/p>\n<p>Here is a typical implementation of the <em>fgets()<\/em> function to read from standard input:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char name[8];\r\n\r\n    printf(\"Your first name: \");\r\n    fgets(name,8,stdin);\r\n    printf(\"Pleased to meet you, %s.\\n\",name);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>fgets()<\/em> statement at Line 8 reads up to 7 characters (one less than 8) from standard input (<code>stdin<\/code>) and stores the string in the <code>name<\/code> buffer. Here&#8217;s a sample run:<\/p>\n<p><code>Your first name: Anabell<br \/>\nPleased to meet you, Anabell.<\/code><\/p>\n<p>Looks ducky, right? The name Anabell is input and output all neat and tidy. But, as you&#8217;ll discover, this output is coincidental; because <em>fgets()<\/em> is stream-oriented, input isn&#8217;t always guaranteed to be neat and tidy.<\/p>\n<p>Consider the input stream illustrated in Figure 1.<\/p>\n<div id=\"attachment_3383\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3383\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure1_stream_input.png\" alt=\"\" width=\"500\" height=\"144\" class=\"size-full wp-image-3383\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure1_stream_input.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure1_stream_input-300x86.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-3383\" class=\"wp-caption-text\">Figure 1. How <em>fgets()<\/em> reads 8 characters of input (7 characters plus the null character).<\/p><\/div>\n<p>This input stream represents text typed at the <code>Your first name:<\/code> prompt. In this case, <strong>Anabell<\/strong> was typed, followed by the Enter key press. That character, newline (<code>\\n<\/code>), terminated input for <em>fegts()<\/em>, which processed and displayed the first 7 characters of input and transformed the newline into the null character to cap the string.<\/p>\n<p>Suppose that the <em>fgets()<\/em> statement is changed to <code>fgets(name,10,stdin)<\/code> in the code and the buffer size updated to 10 characters of storage. With the same input stream illustrated in Figure 1 provided, here is the output:<\/p>\n<p><code>Your first name: Anabell<br \/>\nPleased to meet you, Anabell<br \/>\n.<\/code><\/p>\n<p>In this example, the newline character is read and terminates the input. Figure 2 updates how the stream is read.<\/p>\n<div id=\"attachment_3395\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3395\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure2_stream_input.png\" alt=\"\" width=\"500\" height=\"144\" class=\"size-full wp-image-3395\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure2_stream_input.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure2_stream_input-300x86.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-3395\" class=\"wp-caption-text\">Figure 2.  The <em>fgets()<\/em> function stops reading input when the newline is encountered.<\/p><\/div>\n<p>Though the cut off is at 10 characters, the newline terminated input. Also, the newline appears in the output, which is why the period shows up on the third line.<\/p>\n<p>Now suppose I change the <em>fgets()<\/em> statement to read <code>fgets(name,16,stdin)<\/code> and update the buffer size to 16 as well. Assume that Figure 3 illustrates the input stream.<\/p>\n<div id=\"attachment_3385\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3385\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure3_stream_input.png\" alt=\"\" width=\"500\" height=\"144\" class=\"size-full wp-image-3385\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure3_stream_input.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1201-figure3_stream_input-300x86.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-3385\" class=\"wp-caption-text\">Figure 3. Reading in 16 characters, though the Enter key (newline) was pressed at character 20.<\/p><\/div>\n<p>Here&#8217;s the output:<\/p>\n<p><code>Your first name: Anabell Sweet Kitty<br \/>\nPleased to meet you, Anabell Sweet K.<\/code><\/p>\n<p>The input stream is read up until the 15th character, K. The 16th character is set to <code>\\0<\/code> by the <em>fgets()<\/em> function. The rest of the input stream is ignored. In the output, you see the truncated stream, where the K is the last displayable character. The period appears on the same line because the newline (unread at position 20 in the stream) wasn&#8217;t processed.<\/p>\n<p>I hope these examples and illustrations help you to better understand how the <em>fgets()<\/em> statement processes text input. The method is quite consistent, though it does take some getting used to.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vital to using the  <em>fgets()<\/em> function is understanding how it reads characters from the input stream. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3379\">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-3379","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\/3379","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=3379"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3379\/revisions"}],"predecessor-version":[{"id":3396,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3379\/revisions\/3396"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}