{"id":3575,"date":"2019-04-27T00:01:10","date_gmt":"2019-04-27T07:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3575"},"modified":"2019-04-20T15:19:01","modified_gmt":"2019-04-20T22:19:01","slug":"long-lines","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3575","title":{"rendered":"Long Lines"},"content":{"rendered":"<p>If I were well-versed in a handful of programming languages, I&#8217;m sure I&#8217;d find one better than C for examining line lengths in a text file. Perhaps Perl could do the trick? or even the shell language? Regardless, I know C.<br \/>\n<!--more--><br \/>\nI forget what motivated me to write the long lines utility, which I call <em>width<\/em> on my system. I needed to examine text files and determine the maximum line length. Rather than incorporate a maximum line length function into a larger code, I wrote a filter. The program processes standard input, counting characters in each line. The longest line length is output when the program is done.<\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int ch,count,max;\r\n\r\n    <span class=\"comments\">\/* initialize *\/<\/span>\r\n    count = max = 0;\r\n\r\n    while(1)\r\n    {\r\n        ch = getchar();\r\n        if( ch == EOF )\r\n            break;\r\n        if( ch == '\\n' )\r\n        {\r\n            if( count &gt; max )\r\n                max = count;\r\n            count = 0;\r\n        }\r\n        else\r\n        {\r\n            count++;\r\n        }\r\n    }\r\n    printf(\"Longest line has %d characters.\\n\",max);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Like any C language filter, the program relies upon an endless loop where <em>getchar()<\/em> and <em>putchar()<\/em> statements gobble input and generate output &mdash; though for this code, I don&#8217;t need the output part. Only Line 12 accepts input.<\/p>\n<p>The <em>if<\/em> test at Line 13 checks for the <code>EOF<\/code> (end-of-file marker), which breaks the loop.<\/p>\n<p>The loop checks for the newline character at Line 15. When found, the line has been read and the value of variable <code>count<\/code> is compared with variable <code>max<\/code>. If <code>count<\/code> is greater, it becomes the current maximum line length value. Variable <code>count<\/code> is then reset to zero.<\/p>\n<p>Otherwise, the <em>else<\/em> condition increments variable <code>count<\/code>, tallying another character in the line.<\/p>\n<p>After the file is read, a <em>printf()<\/em> statement outputs the results, the value of variable <code>max<\/code>.<\/p>\n<p>Here&#8217;s a sample run with standard input:<\/p>\n<p><code>$ .\/a.out<br \/>\n123<br \/>\n1234567890<br \/>\n23<br \/>\nLongest line has 10 characters.<\/code><\/p>\n<p>And another when processing the source code file as input:<\/p>\n<p><code>$ .\/a.out &lt; 2019_04_27-Lesson-a.c<br \/>\nLongest line has 49 characters.<\/code><\/p>\n<p>Another useful tidbit of information would be the number of the longest line. To implement this modification, I&#8217;ve added a variable <code>line<\/code> initialized to zero. It increments with each newline. When the new value for <code>max<\/code> is set, a second value <code>maxline<\/code> is also set to reflect the longest line.<\/p>\n<p>Here is the updated code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int ch,count,max,line,maxline;\r\n\r\n    <span class=\"comments\">\/* initialize *\/<\/span>\r\n    count = max = maxline = 0;\r\n    line = 1;\r\n\r\n    while(1)\r\n    {\r\n        ch = getchar();\r\n        if( ch == EOF )\r\n            break;\r\n        if( ch == '\\n' )\r\n        {\r\n            if( count &gt; max )\r\n            {\r\n                max = count;\r\n                maxline = line;\r\n            }\r\n            count = 0;\r\n            line++;\r\n        }\r\n        else\r\n        {\r\n            count++;\r\n        }\r\n    }\r\n    printf(\"Line %d has %d characters.\\n\",maxline,max);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>And sample output using the above source code:<\/p>\n<p><code>$ .\/a.out &lt; 2019_04_27-Lesson-b.c<br \/>\nLine 31 has 52 characters.<\/code><\/p>\n<p>In my actual <em>width<\/em> program, other options are available: I have the code sift through command line switches to output maximum and minimum lines, as well as output the line itself. All this overhead makes me wonder again why I originally needed the utility or whether I was just bored and kept coding the utility. Probably it was the latter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a utility I whipped up to discover the longest line in a text file. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3575\">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-3575","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\/3575","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=3575"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3575\/revisions"}],"predecessor-version":[{"id":3577,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3575\/revisions\/3577"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}