{"id":3337,"date":"2018-10-27T00:01:55","date_gmt":"2018-10-27T07:01:55","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3337"},"modified":"2018-10-20T12:41:52","modified_gmt":"2018-10-20T19:41:52","slug":"reading-and-writing-values","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3337","title":{"rendered":"Reading and Writing Values"},"content":{"rendered":"<p>Writing a value to a file and reading it from a file work exactly like reading and writing values from standard input and output. File-based versions of standard I\/O functions are used, so the process should be familiar to you. Still, there&#8217;s an interesting catch.<br \/>\n<!--more--><br \/>\nThe key to reading values is knowing the file&#8217;s format. You could use the example from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3330\">last week&#8217;s Lesson<\/a> to scan the file input buffer for values, but the operation works best when you know a specific value lurks within a file and how that value is formatted, as raw data or as text.<\/p>\n<p>In the following code, the value 1088 is written to the file <code>highscore.dat<\/code>. The <em>fprintf()<\/em> function writes the value by using a format string &mdash; familiar stuff. The only addition is the file&#8217;s <code>FILE<\/code> variable handle, which is the function&#8217;s first argument. To read back the value, the <em>fscanf()<\/em> function is used. Like <em>fprintf()<\/em>, its argument list also sports a file handle variable.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const char filename[] = \"highscore.dat\";\r\n    int score = 1088;\r\n    FILE *hs;\r\n\r\n    <span class=\"comments\">\/* create the file and write the value *\/<\/span>\r\n    printf(\"Writing high score: %d\\n\",score);\r\n    hs = fopen(filename,\"w\");\r\n    if( hs == NULL)\r\n    {\r\n        fprintf(stderr,\"Error writing to %s\\n\",filename);\r\n        return(1);\r\n    }\r\n    fprintf(hs,\"%d\",score);\r\n    fclose(hs);\r\n\r\n    <span class=\"comments\">\/* open the file and read the value *\/<\/span>\r\n    hs = fopen(filename,\"r\");\r\n    if( hs == NULL)\r\n    {\r\n        fprintf(stderr,\"Error reading from  %s\\n\",filename);\r\n        return(1);\r\n    }\r\n    fscanf(hs,\"%d\",&amp;score);\r\n    printf(\"Reading high score: %d\\n\",score);\r\n    fclose(hs);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Aside from all the checks to ensure files are opened properly, this code isn&#8217;t that complex: A file is opened, the <em>fprintf()<\/em> statement at Line 17 sends the value of variable <code>score<\/code> out to the file. The file is closed.<\/p>\n<p>After the file is opened for reading at Line 21, the <em>fscanf()<\/em> function reads the file&#8217;s text for an integer value, storing the result in the <code>score<\/code> variable.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Writing high score: 1088<br \/>\nReading high score: 1088<\/code><\/p>\n<p>The value written to and read from the file is stored as text &mdash; just as it would be had this program read from standard input and written to standard output. On my computer, the contents of the file <code>highscore.dat<\/code> are the characters 1, 0, 8, and 8. Because a newline wasn&#8217;t written to the file, it&#8217;s not stored.<\/p>\n<p>If you modify the code to write a real number to the file, it&#8217;s also stored as plain text.<\/p>\n<p>To write raw data to a file and then read that raw data back into a variable, you must use different file I\/O statements. I cover this topic in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just as you can read and write values from standard input and output, you can read and write values from and to files. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3337\">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-3337","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\/3337","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=3337"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3337\/revisions"}],"predecessor-version":[{"id":3351,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3337\/revisions\/3351"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}