{"id":1112,"date":"2015-01-03T00:01:44","date_gmt":"2015-01-03T08:01:44","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1112"},"modified":"2022-07-21T07:04:17","modified_gmt":"2022-07-21T14:04:17","slug":"the-getline-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1112","title":{"rendered":"The <em>getline()<\/em> Function"},"content":{"rendered":"<p>The latest and most trendy function for reading a string of text is <em>getline()<\/em>. It&#8217;s a new C library function, having appeared around 2010 or so.<br \/>\n<!--more--><br \/>\nYou might not have heard of the <em>getline()<\/em> function, and a few C programmers avoid it because it uses &#8212; brace yourself &#8212; <em>pointers<\/em>! Even so, it&#8217;s a good line-input function, and something you should be familiar with, even if you don&#8217;t plan on using it.<\/p>\n<p>Here&#8217;s a typical <em>getline()<\/em> statement:<\/p>\n<pre><code>getline(&buffer,&size,stdin);<\/code><\/pre>\n<p>The <em>getline()<\/em> function is prototyped in the <code>stdio.h<\/code> header file. Here are the three arguments:<\/p>\n<p><code>&buffer<\/code> is the address of the first character position where the input string will be stored. It&#8217;s <em>not<\/em> the base address of the buffer, but of the first <em>character<\/em> in the buffer. This pointer type (a pointer-pointer or the <code>**<\/code> thing) causes massive confusion.<\/p>\n<p><code>&size<\/code> is the address of the variable that holds the size of the input buffer, another pointer.<\/p>\n<p><code>stdin<\/code> is the input file handle. So you could use <em>getline()<\/em> to read a line of text from a file, but when <code>stdin<\/code> is specified, standard input is read.<\/p>\n<p>Here&#8217;s a sample program:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char *buffer;\r\n    size_t bufsize = 32;\r\n    size_t characters;\r\n\r\n    buffer = (char *)malloc(bufsize * sizeof(char));\r\n    if( buffer == NULL)\r\n    {\r\n        perror(\"Unable to allocate buffer\");\r\n        exit(1);\r\n    }\r\n\r\n    printf(\"Type something: \");\r\n    characters = getline(&buffer,&bufsize,stdin);\r\n    printf(\"%zu characters were read.\\n\",characters);\r\n    printf(\"You typed: '%s'\\n\",buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The string input is stored at the memory location referenced by pointer <code>buffer<\/code>, declared at Line 8.<\/p>\n<p>Lines 9 and 10 use the <em>size_t<\/em> variable type, which is a special type of integer. That&#8217;s required by the <em>getline()<\/em> function. The buffer size is 32 characters, which is set at Line 9. It must be referenced as a pointer, not a literal value.<\/p>\n<p>In this code, 32 bytes of storage are assigned to memory location <code>buffer<\/code> via the <em>malloc()<\/em> function at Line 12. Lines 13 through 17 handle the (rare) condition when memory isn&#8217;t available. Odds are low that would happen in this program, but it&#8217;s good programming practice to check.<\/p>\n<p>The <em>getline()<\/em> function debuts at Line 20. It uses the address of <code>buffer<\/code>, <code>bufsize<\/code>, and then <code>stdin<\/code> for standard input.<\/p>\n<p>Because variable <code>characters<\/code> is a <em>size_t<\/em> variable type, the <code>%zu<\/code> placeholder is used in the <em>printf()<\/em> function at Line 21.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>Type something: Today is January 3, 2015\r\n25 characters were read.\r\nYou typed: 'Today is January 3, 2015\r\n'<\/code><\/pre>\n<p>Notice anything annoying with the output?<\/p>\n<p>Yep, as with the <em>fgets()<\/em> function, <em>getline()<\/em> reads and stores the newline character as part of the string. So if the pointer thing bothers you, just use <em>fgets()<\/em> instead.<\/p>\n<p>Finally, if you&#8217;re curious, you can use <em>getline()<\/em> with array notation for the storage buffer. It ain&#8217;t pretty, but it works:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint input(char *s,int length);\r\n\r\nint main()\r\n{\r\n    char buffer[32];\r\n    char *b = buffer;\r\n    size_t bufsize = 32;\r\n    size_t characters;\r\n\r\n    printf(\"Type something: \");\r\n    characters = getline(&b,&bufsize,stdin);\r\n    printf(\"%zu characters were read.\\n\",characters);\r\n    printf(\"You typed: '%s'\\n\",buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>To get the <code>**<\/code> type of pointer from array notation, you need to declare a pointer variable and assign it to the array, which is done above in Line 8. You can then use that pointer with the ampersand in the <em>getline()<\/em> function, shown above at Line 13.<\/p>\n<p><strong>2022, July 9:<\/strong> Updated post with some notes and corrections <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5432\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C library just can&#8217;t seem to get right string input. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1112\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1112","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\/1112","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=1112"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1112\/revisions"}],"predecessor-version":[{"id":5464,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1112\/revisions\/5464"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}