{"id":1563,"date":"2015-09-26T00:01:38","date_gmt":"2015-09-26T07:01:38","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1563"},"modified":"2016-12-25T08:23:49","modified_gmt":"2016-12-25T16:23:49","slug":"more-file-access-messing","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1563","title":{"rendered":"More File Access Messing"},"content":{"rendered":"<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1552\">last week&#8217;s Lesson<\/a>, I covered the <em>ftell()<\/em> function, which returns the current file position indicator. That indicator can be manipulated, allowing you to control how a file is read in a non-sequential way.<br \/>\n<!--more--><br \/>\nNormally, file access is sequential: One byte is read after another. As the operating system coughs up those bytes, it uses a file position indicator to keep track of the information read. If you don&#8217;t mess with the indicator, the file is read front-to-back. If you mess with the indicator, you can read a file in any order.<\/p>\n<p>One function that drastically messes with the file position indicator is <em>rewind()<\/em>. It resets the indicator back to zero, effectively starting over. The function takes a single argument, the file pointer variable and it&#8217;s declared in the <code>stdio.h<\/code> header.<\/p>\n<p>The following sample code is based on the example from last week&#8217;s Lesson. It uses the file <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/09\/gettysburg.txt\"><code>gettysburg.txt<\/code><\/a>, which you can obtain by clicking the filename link.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    FILE *fh;\r\n    int c;\r\n\r\n    <span class=\"comments\">\/* open the file *\/<\/span>\r\n    fh = fopen(\"gettysburg.txt\",\"r\");\r\n    if(fh == NULL)\r\n    {\r\n        perror(\"Unable to open file\\n\");\r\n        return(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* read the file and display *\/<\/span>\r\n    while( (c=fgetc(fh)) != EOF)\r\n        putchar(c);\r\n    rewind(fh);\r\n    while( (c=fgetc(fh)) != EOF)\r\n        putchar(c);\r\n    \r\n    <span class=\"comments\">\/* close the file *\/<\/span>\r\n    fclose(fh);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>rewind()<\/em> function resets the file position indicator at Line 22. Lines 23 and 24 are duplicates of the <em>while<\/em> loop at Lines 20 and 21. So even though the file indicator is at the EOF, the <em>rewind()<\/em> function resets that pointer and starts over, effectively re-reading the file.<\/p>\n<p>The end result of this sample code is that the same information is read and output twice, but without the need to close and re-open that file.<\/p>\n<p>The <em>rewind()<\/em> function is absolute. It always resets the file position indicator back to the first byte in a file. If you want to set the indicator to a specific position within the file, you use the <em>fseek()<\/em> function. I&#8217;ll cover that function in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1572\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The file position indicator can be manipulated in interesting and cruel ways. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1563\">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-1563","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\/1563","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=1563"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1563\/revisions"}],"predecessor-version":[{"id":2286,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1563\/revisions\/2286"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}