{"id":4068,"date":"2020-04-11T00:01:38","date_gmt":"2020-04-11T07:01:38","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4068"},"modified":"2020-04-04T09:17:01","modified_gmt":"2020-04-04T16:17:01","slug":"crossing-between-formatted-and-unformatted-file-functions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4068","title":{"rendered":"Crossing Between Formatted and Unformatted File Functions"},"content":{"rendered":"<p>One of the reasons behind the low-level <em>open()<\/em> function is to access non-traditional files. This stems from the UNIX environment&#8217;s treatment of every device as a file. Sometimes you need low-level access to accomplish specific tasks, such as accessing a device driver. Yet, in the C language universe, an interesting crossover is provided between low-level raw file access and formatted file access.<br \/>\n<!--more--><br \/>\nThe function that bridges the gap is <em>fdopen()<\/em>. Here is its format:<\/p>\n<p><code>FILE * fdopen(int fildes, const char *mode);<\/code><\/p>\n<p>This function works like the <em>fopen()<\/em> function, but substituting a file descriptor integer, <code>fildes<\/code>, for the filename (first) argument. The <code>fildes<\/code> is the integer value returned from an <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4036\"><em>open()<\/em> function<\/a>. The second argument is a string representing the file-opening mode. The value returned is a <em>FILE<\/em> pointer.<\/p>\n<p>After the <em>fdopen()<\/em> function is successfully used on a file opened by using the <em>open()<\/em> function, you can use employ the familiar formatted file functions to work with the file. When you&#8217;re done, you first <em>fclose()<\/em> the file that <em>fdopen()<\/em> opened, then <em>close()<\/em> the file the <em>open()<\/em> function opened. Yes, the process involves more overhead, but that&#8217;s how it works. Sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.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    FILE *fp;\r\n    int fdes,ch;\r\n\r\n    <span class=\"comments\">\/* open the file to get a file descriptor *\/<\/span>\r\n    fdes = open(filename,O_RDONLY);\r\n    if( fdes==-1 )\r\n    {\r\n        fprintf(stderr,\"Unable to open %s for unformatted input\\n\",filename);\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* use the descriptor to open the file for streamed input *\/<\/span>\r\n    fp = fdopen(fdes,\"r\");\r\n    if( fp==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to open %s for streamed input\\n\",filename);\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* read the file using formatted input *\/<\/span>\r\n    while( !feof(fp) )\r\n    {\r\n        ch = fgetc(fp);\r\n        if( ch==EOF )\r\n            break;\r\n        putchar(ch);\r\n    }\r\n\r\n    <span class=\"comments\">\/* close the open files *\/<\/span>\r\n    fclose(fp);\r\n    close(fdes);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code first uses the <em>open()<\/em> function at Line 13 to open <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/09\/gettysburg.txt\">gettysburg.txt<\/a>. The file descriptor is stored in <em>int<\/em> variable <code>fdes<\/code>.<\/p>\n<p>Next, the <em>fdopen()<\/em> function opens the file, already open for unformatted reading, for formatted reading:<\/p>\n<p><code>fp = fdopen(fdes,\"r\");<\/code><\/p>\n<p>The <code>fdes<\/code> variable is used instead of a filename. The result is saved in the <code>fp<\/code> <em>FILE<\/em> pointer, which is immediately tested. If everything is kosher, the <em>fgetc()<\/em> function reads the file&#8217;s contents and outputs its text:<\/p>\n<p><code>while( !feof(fp) )<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;ch = fgetc(fp);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if( ch==EOF )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;putchar(ch);<br \/>\n}<\/code><\/p>\n<p>The <em>feof()<\/em> function and a test for the <code>EOF<\/code> character are used to determine when all the text is read from the file. These are formatted file-reading techniques, yet applied to a file originally opened for low-level access.<\/p>\n<p>Two statements are required to close-up when done:<\/p>\n<p><code>fclose(fp);<br \/>\nclose(fdes);<\/code><\/p>\n<p>A sample run output text from the <code>gettysburg.txt<\/code> file.<\/p>\n<p>The <em>fdopen()<\/em> function provides a useful way to use your favorite file access functions on files opened for low-level access. Do be aware that the <em>fdopen()<\/em> function must be used in the same mode as the file was opened with in the <em>open()<\/em> function. Further, some file-opening mode strings don&#8217;t work on files opened in-the-raw: The <code>\"x\"<\/code> mode causes <em>fdopen()<\/em> to fail if the file opened doesn&#8217;t exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s possible to use the raw-access <em>open()<\/em> function to open a file and then use formatted file functions. Weird, but possible. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4068\">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-4068","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\/4068","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=4068"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4068\/revisions"}],"predecessor-version":[{"id":4073,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4068\/revisions\/4073"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}