{"id":4046,"date":"2020-03-28T00:01:01","date_gmt":"2020-03-28T07:01:01","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4046"},"modified":"2020-03-21T12:12:27","modified_gmt":"2020-03-21T19:12:27","slug":"raw-reading-file-data","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4046","title":{"rendered":"Raw Reading File Data"},"content":{"rendered":"<p>When you use the <em>fopen()<\/em> function to open a file for reading, a buncha functions are available for reading data: <em>fread()<\/em>, <em>fgets()<\/em>, <em>fgetc()<\/em>, and others I&#8217;m too lazy to look up. Reading files by using <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4036\">the <em>open()<\/em> function<\/a>, however, gives you this choice: the <em>read()<\/em> function.<br \/>\n<!--more--><br \/>\nThe <em>read()<\/em> function has this <em>man<\/em> page format (from page 2), the function is prototyped in the <code>unistd.h<\/code> header file:<\/p>\n<p><code>ssize_t read(int fildes, void *buf, size_t nbyte);<\/code><\/p>\n<p>The first argument, <code>fildes<\/code>, is the file descriptor integer returned from the <em>open()<\/em> function.<\/p>\n<p>Argument <code>buf<\/code> is the address of a buffer to store the data read. It can be whatever type of data is required: character, structure, what-have-you.<\/p>\n<p>The final argument, <code>nbyte<\/code>, is the number of bytes to read, usually the size of the <code>buf<\/code> (the buffer).<\/p>\n<p>The return value is the number of bytes actually read.<\/p>\n<p>Like other file-reading functions, <em>read()<\/em> consumes file data sequentially. A file indicator keeps track of the location in the file from which data is read. This indicator, or &#8220;pointer,&#8221; can be manipulated to allow for random file access.<\/p>\n<p>It&#8217;s important to keep in mind that the <em>read()<\/em> function&#8217;s data is unformatted. It does not read strings or any data type, but rather raw bytes. The function cares not when the null character terminates a string, the size of an <em>int<\/em>, or anything a formatted file input function would concern itself with. It sees everything as unformatted data.<\/p>\n<p>The following code uses the <em>read()<\/em> function to consume the raw bytes from the file <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/09\/gettysburg.txt\">gettysburg.txt<\/a>.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;fcntl.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\n#define SIZE 2048\r\n\r\nint main()\r\n{\r\n    const char filename[] = \"gettysburg.txt\";\r\n    int fdes,x;\r\n    char buffer[SIZE];\r\n    size_t r;\r\n\r\n    <span class=\"comments\">\/* open the file for unformatted input *\/<\/span>\r\n    fdes = open(filename,O_RDONLY);\r\n    if( fdes==-1 )\r\n    {\r\n        fprintf(stderr,\"Unable to open %s\\n\",filename);\r\n        return(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* read raw data *\/<\/span>\r\n    r = read( fdes, buffer, SIZE );\r\n    \r\n    <span class=\"comments\">\/* output the buffer *\/<\/span>\r\n    <span class=\"comments\">\/* not null character terminated! *\/<\/span>\r\n    for( x=0; x&lt;r; x++ )\r\n        putchar( buffer[x] );\r\n\r\n    <span class=\"comments\">\/* close the file *\/<\/span>\r\n    close(fdes);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 23 reads a buffer-sized chunk of data from the file:<\/p>\n<p><code>r = read( fdes, buffer, SIZE );<\/code><\/p>\n<p>I know that the file size is less than the value of <code>SIZE<\/code>, so a single <em>read()<\/em> function gobbles its entire contents. The data is stored in <em>char<\/em> array <code>buffer[]<\/code>, and then spewed out in the <em>for<\/em> loop at Line 27:<\/p>\n<p><code>for( x=0; x&lt;r; x++ )<\/code><\/p>\n<p>The loop uses the value of <code>r<\/code>, returned from the <em>read()<\/em> function, to set its limit. The file&#8217;s contents are output, but the code isn&#8217;t perfect because it wouldn&#8217;t output all the file&#8217;s data if its size is larger than defined constant <code>SIZE<\/code>, or 2048 bytes.<\/p>\n<p>The following code code improves upon this file-reading example, modifying the <em>read()<\/em> function to fetch a single byte at a time from the open file:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;fcntl.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    const char filename[] = \"gettysburg.txt\";\r\n    int fdes;\r\n    char buffer[1];\r\n    size_t r;\r\n\r\n    <span class=\"comments\">\/* open the file for unformatted input *\/<\/span>\r\n    fdes = open(filename,O_RDONLY);\r\n    if( fdes==-1 )\r\n    {\r\n        fprintf(stderr,\"Unable to open %s\\n\",filename);\r\n        return(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* output the buffer *\/<\/span>\r\n    while(1)\r\n    {\r\n        <span class=\"comments\">\/* read raw data one byte at a time *\/<\/span>\r\n        r = read( fdes, buffer, 1 );\r\n        if( r==0 )    <span class=\"comments\">\/* end of file *\/<\/span>\r\n            break;\r\n        if( r==-1 )    <span class=\"comments\">\/* file read error *\/<\/span>\r\n        {\r\n            fprintf(stderr,\"File read error\\n\");\r\n            break;\r\n        }\r\n        putchar( buffer[0] );\r\n    }\r\n\r\n    <span class=\"comments\">\/* close the file *\/<\/span>\r\n    close(fdes);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>read()<\/em> function at Line 24 grabs only one byte:<\/p>\n<p><code>r = read( fdes, buffer, 1 );<\/code><\/p>\n<p>The byte read  must be stored in a buffer, a <em>char<\/em> array or pointer, which has only one character of storage as defined at Line 9:<\/p>\n<p><code>char buffer[1];<\/code><\/p>\n<p>The <em>while<\/em> loop that reads the data (starting at Line 21) is endless. To terminate the loop, the value <code>r<\/code> returned from the <em>read()<\/em> function must be equal to either 0 or -1: 0 indicates the file has been completely read and -1 indicates a read error.<\/p>\n<p>This second example is better than the first because it reads file data no matter what the file&#8217;s size.<\/p>\n<p>In next week&#8217;s Lesson I cover writing raw data to a file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just as the <em>open()<\/em> function is different from <em>fopen()<\/em>, the functions to read and write data to a file are different as well. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4046\">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-4046","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\/4046","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=4046"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4046\/revisions"}],"predecessor-version":[{"id":4055,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4046\/revisions\/4055"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}