{"id":24,"date":"2013-06-01T00:01:45","date_gmt":"2013-06-01T08:01:45","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=24"},"modified":"2018-10-17T08:24:14","modified_gmt":"2018-10-17T15:24:14","slug":"fixing-fgets-for-input","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=24","title":{"rendered":"Fixing <em>fgets()<\/em> for Input"},"content":{"rendered":"<p>The <em>fgets()<\/em> function is a marvelous tool for reading chunks of text from the keyboard or standard input. One problem I have with it is that it also reads the Enter key press, sticking a <code>\\n<\/code> into the string. That&#8217;s not a big issue, however, because you can easily pull out that character.<br \/>\n<!--more--><br \/>\nThe newline is a required character for breaking standard input into chunks. It makes sense that <em>fgets()<\/em> would accept it and store it. Often times, however, you don&#8217;t want the newline stored as part of the input.<\/p>\n<p>The solution is to code a function that modifies <em>fgets()<\/em>&#8216;s behavior. The function needs to be passed the address of an input buffer and a length, similar to the original arguments for <em>fgets()<\/em>. The only thing you don&#8217;t need to pass is the file handle; for purposes of standard input, the <code>stdin<\/code> handle can be assumed.<\/p>\n<p>I&#8217;ve named by substitute function <em>input()<\/em>, as shown in this code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid input(char *string,int length);\r\n\r\nint main()\r\n{\r\n    char firstname[32],lastname[32];\r\n\r\n    printf(\"What is your first name? \");\r\n    input(firstname,32);\r\n    printf(\"What is your last name? \");\r\n    input(lastname,32);\r\n    printf(\"Please to meet you %s %s.\\n\",\r\n        firstname,\r\n        lastname);\r\n\r\n    return(0);\r\n}\r\n\r\nvoid input(char *string,int length)\r\n{\r\n    int x;\r\n\r\n    fgets(string,length,stdin);\r\n    for(x=0;x&lt;=length;x++)\r\n    {\r\n        if( string[x] == '\\n')\r\n        {\r\n            string[x] = '\\0';\r\n            break;\r\n        }\r\n    }\r\n}<\/pre>\n<p>The <em>input()<\/em> function at Line 20 serves as the surrogate for <em>fgets()<\/em>. In fact, <em>fgets<\/em>() is called immediately within the function, at Line 24. Then a <code>for<\/code> loop plows through input searching for the newline. Once found, it&#8217;s replaced with a null character, <code>\\0<\/code>, at Line 29.<\/p>\n<p>Remember: It&#8217;s possible that the input string may not contain a newline, which is why I chose to use a <code>for<\/code> loop and work through all of the input buffer.<\/p>\n<p>Also see <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2631\">this post<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a solution for fixing the newline problem when using the <em>fgets()<\/em> function for text input. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=24\">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-24","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\/24","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=24"}],"version-history":[{"count":11,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":3341,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/3341"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}