{"id":6514,"date":"2024-08-08T00:01:52","date_gmt":"2024-08-08T07:01:52","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6514"},"modified":"2024-08-03T11:11:17","modified_gmt":"2024-08-03T18:11:17","slug":"the-seventh-line-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6514","title":{"rendered":"The Seventh Line &#8211; Solution"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6494\">month&#8217;s programming Exercise<\/a> isn&#8217;t as much about file access as it&#8217;s about dealing with a situation when no data is available. The task: Read the seventh line from a file.<br \/>\n<!--more--><br \/>\nThe  approach here is to use sequential file access, which is a topic I mentioned in the Exercise post. The file is read one line at a time, tracking count of the lines as each is read. In my solution, I opted for a <code>BUFSIZ<\/code> buffer, which is plenty huge enough to swallow long lines in a text file. Here&#8217;s my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_08-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_08-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main( int argc, char *argv[] )\r\n{\r\n    FILE *fh;\r\n    char buffer[BUFSIZ],*r,*filename;\r\n    int line;\r\n\r\n    <span class=\"comments\">\/* check for the command line argument *\/<\/span>\r\n    if( argc&lt;2 )\r\n    {\r\n        fprintf(stderr,\"Please specify a file to examine\\n\");\r\n        return 1;\r\n    }\r\n\r\n    <span class=\"comments\">\/* open the file *\/<\/span>\r\n    filename = argv[1];        <span class=\"comments\">\/* for readability *\/<\/span>\r\n    fh = fopen(filename,\"r\");\r\n    if ( fh==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to open file '%s'\\n\",filename);\r\n        return 1;\r\n    }\r\n\r\n    <span class=\"comments\">\/* read seven lines *\/<\/span>\r\n    for( line=0; line&lt;7; line++ )\r\n    {\r\n        r = fgets(buffer,BUFSIZ,fh);\r\n        if( r==NULL )\r\n        {\r\n            printf(\"File '%s' lacks a seventh line\\n\",filename);\r\n            fclose(fh);\r\n            return 1;\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* output result *\/<\/span>\r\n    printf(\"The seventh line of file '%s' is:\\n%s\",\r\n            filename,\r\n            buffer\r\n          );\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    fclose(fh);\r\n    return 0;\r\n}<\/pre>\n<p>The code first checks for a command line argument, which is assumed to be a text file to open. When missing, an error message is output and the program ends.<\/p>\n<p>When a filename is supplied, it&#8217;s opened and read. A <em>for<\/em> loop counts lines from zero through six, with six being the &#8220;seventh&#8221; line.<\/p>\n<p>The <em>fgets()<\/em> function fetches each line, storing it in <em>char<\/em> array <code>buffer[]<\/code>. This function returns NULL when the line isn&#8217;t read (probably the end-of-file), in which case an appropriate message is output, the file is closed, and the program ends.<\/p>\n<p>After the loop is complete, the seventh line of the file is stored in <code>buffer<\/code>. It&#8217;s output. The file is closed. The program ends.<\/p>\n<p>Here&#8217;s a successful sample run of my solution:<\/p>\n<p><code>.\/a.out declaration.txt<br \/>\nThe seventh line of file 'declaration.txt' is:<br \/>\nHe has refused to pass other Laws for the accommodation of large districts of people, unless those people would relinquish the right of Representation in the Legislature, a right inestimable to them and formidable to tyrants only.<\/code><\/p>\n<p>The code is able to handle the long line of text. Here&#8217;s a run when the file lacks a seventh line:<\/p>\n<p><code>.\/a.out gettysburg.txt<br \/>\nFile 'gettysburg.txt' lacks a seventh line<\/code><\/p>\n<p>If the seventh line is blank, then a blank line is output. I suppose I could have tested for this condition and output a message, but I didn&#8217;t bother.<\/p>\n<p>Finally, a test when the user forgets to supply a filename argument:<\/p>\n<p><code>.\/a.out<br \/>\nPlease specify a file to examine<\/code><\/p>\n<p>The key to this solution is the <em>fgets()<\/em> function, which reads in lines of text and returns NULL on an error or the EOF. The challenge is to handle the error conditions of missing filenames and text files without a seventh line. I hope your solution met with success. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s programming Exercise isn&#8217;t as much about file access as it&#8217;s about dealing with a situation when no data is available. The task: Read the seventh line from a file.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6514","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6514","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=6514"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6514\/revisions"}],"predecessor-version":[{"id":6530,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6514\/revisions\/6530"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}