{"id":3314,"date":"2018-10-06T00:01:40","date_gmt":"2018-10-06T07:01:40","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3314"},"modified":"2018-10-13T10:25:43","modified_gmt":"2018-10-13T17:25:43","slug":"a-file-outta-nowhere","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3314","title":{"rendered":"A File Outta Nowhere"},"content":{"rendered":"<p>I had such a struggle with file I\/O when I first learned computer programming. I knew what a file was and how to create it in an application. With computer programming, however, you enter a lower-level realm that requires more knowledge of file access.<br \/>\n<!--more--><br \/>\nThe struggle is between sequential and random file access. Before boring into the details, I present the basic file access elements in the C language:<\/p>\n<p><strong>FILE<\/strong> To access a file, you use a file handle, represented by the <code>FILE<\/code> variable. <code>FILE<\/code> is typedef&#8217;d in the <code>stdio.h<\/code> header file as a structure pointer. <code>FILE<\/code> variables are created with the * operator, but afterwards used without that operator. That way programmers who can&#8217;t stand pointers can work with file functions and not suffer an aneurism.<\/p>\n<p><strong><em>fopen()<\/em><\/strong> The function to open a file for reading and writing is <em>fopen()<\/em>. Its arguments are two strings, a filename and an access mode. This function is defined in the <code>stdio.h<\/code> header file.<\/p>\n<p><strong><em>fclose()<\/em><\/strong> The function to close a file is <em>fclose()<\/em>. It writes any left over tidbits to the file and ensures that data is safely stored. Its sole argument is the <code>FILE<\/code> variable returned from a previously successful <em>fopen()<\/em> function. It too is defined in <code>stdio.h<\/code>.<\/p>\n<p>Of these three elements, <em>fopen()<\/em> is the most involved &mdash; which is good, because it&#8217;s not that complex. Its second argument is a mode string that stipulates how the file is opened:<\/p>\n<p><code>\"a\"<\/code> to open or create a file for appending.<br \/>\n<code>\"r\"<\/code> to open a file for reading only.<br \/>\n<code>\"w\"<\/code> to open or create a file for writing.<\/p>\n<p>Both <code>\"a\"<\/code> and <code>\"w\"<\/code> create a file if it doesn&#8217;t exist. The <code>\"r\"<\/code> option fails if the file doesn&#8217;t exist.<\/p>\n<p>Appending a <code>+<\/code> character to any mode string also opens the file for reading and writing.<\/p>\n<p>Following <code>\"w\"<\/code> or <code>\"w+\"<\/code> with the <code>x<\/code> character causes the <em>fopen()<\/em> function to fail when the named file already exists.<\/p>\n<p>None of these options set how the data is read from the file, sequentially or via random access. So you can put off being flustered by those options until later.<\/p>\n<p>The following code shows a file-opening skeleton. The file is opened for writing, which means it&#8217;s created. If the file indicated already exists, it&#8217;s overwritten.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const char filename[] = \"test\";\r\n    FILE *f;\r\n\r\n    printf(\"Creating '%s'\\n\",filename);\r\n    f = fopen(filename,\"w\");\r\n    if( f == NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to create file '%s'\\n\",filename);\r\n        return(1);\r\n    }\r\n    fclose(f);\r\n    puts(\"Done\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The filename <code>test<\/code> is established a string constant at Line 5. Line 6 creates the <code>FILE<\/code> handle (pointer variable) <code>f<\/code>.<\/p>\n<p>In Line 9, the <em>fopen()<\/em> function attempts to open the file. Lines 10 through 14 handle any file errors. You <em>must<\/em> include this type of testing in all your file-handling code. When the file pointer is <code>NULL<\/code>, the file cannot be opened (for whatever reason) and you should discontinue accessing it.<\/p>\n<p>Line 15 uses the <em>fclose()<\/em> function to close the file referenced by <code>FILE<\/code> handle <code>f<\/code>.<\/p>\n<p>No data is written to the file <code>test<\/code>. If you check the directory, you see that file <code>test<\/code> has zero bytes. That&#8217;s exactly what the program did: Create a file for writing, not write anything, then close the file.<\/p>\n<p>To alter the code to open a file for reading, change the <code>\"w\"<\/code> argument in Line 10 to <code>\"r\"<\/code>. You can modify the <em>printf()<\/em> statement at Line 8 and the <em>fprintf()<\/em> statement as Line 12 to reflect that a file is being read from and opened for reading. As long as the file test exists, it can be opened and closed.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3321\">next week&#8217;s Lesson<\/a>, I cover writing to a file and reading from a file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making a file is easy, providing that you know the proper incantations. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3314\">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-3314","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\/3314","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=3314"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3314\/revisions"}],"predecessor-version":[{"id":3336,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3314\/revisions\/3336"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}