{"id":4052,"date":"2020-04-04T00:01:27","date_gmt":"2020-04-04T07:01:27","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4052"},"modified":"2020-05-16T08:10:02","modified_gmt":"2020-05-16T15:10:02","slug":"writing-file-data-in-the-raw","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4052","title":{"rendered":"Writing File Data in the Raw"},"content":{"rendered":"<p>Along with <em>open()<\/em> as a raw file access function comes raw functions to read and write data. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4046\">Last week&#8217;s Lesson<\/a> covered the <em>read()<\/em> function. This week covers its twin sibling, <em>write()<\/em>.<br \/>\n<!--more--><br \/>\nAs with many companion functions in the C library, <em>write()<\/em> uses pretty much the same arguments as the <em>read()<\/em> function:<\/p>\n<p><code>ssize_t write(int fildes, const void *buf, size_t nbyte);<\/code><\/p>\n<p>The <code>fildes<\/code> argument is a file descriptor integer returned from an <em>open()<\/em> function, one that opens a file for writing. The <code>buf<\/code> argument is a buffer containing the data to be written, with <code>nbyte<\/code> the size of the data chunk. The <code>buf<\/code> argument is <em>const void<\/em>; <em>const<\/em> implies the buffer shouldn&#8217;t change as data is written and <em>void<\/em> allows you to typecast any data chunk for writing.<\/p>\n<p>The value returned is the actual number of bytes written, which should match <code>nbyte<\/code>. When the value returned is -1, an error has occurred and the global variable <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1735\">errno<\/a> is set.<\/p>\n<p>When the <em>open()<\/em> function opens a file for writing, the <code>O_WRONLY<\/code> <em>oflag<\/em> constant is used. To create the file, you logically OR this flag with the <code>O_CREAT<\/code> defined constant, as shown in the following code:<\/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    char filename[] = \"file_output.txt\";\r\n    char text[] = \"What a lovely little file\\n\";\r\n    int fdes,r;\r\n\r\n    <span class=\"comments\">\/* open the file for writing and creating *\/<\/span>\r\n    fdes = open(filename,O_WRONLY|O_CREAT);\r\n    if( fdes==-1 )\r\n    {\r\n        fprintf(stderr,\"Unable to create %s\\n\",filename);\r\n        return(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* write the buffer *\/<\/span>\r\n    r = write( fdes, text, sizeof(text)-1 );\r\n    if( r != sizeof(text)-1 )\r\n    {\r\n        fprintf(stderr,\"Some kind of file writing error\\n\");\r\n    }\r\n    <span class=\"comments\">\/* confirm success *\/<\/span>\r\n    printf(\"Data written to %s\\n\",filename);\r\n\r\n    <span class=\"comments\">\/* close 'er up *\/<\/span>\r\n    close(fdes);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 12 creates the file <code>file_output.txt<\/code> for writing:<\/p>\n<p><code>fdes = open(filename,O_WRONLY|O_CREAT);<\/code><\/p>\n<p>At Line 20, the string text is written to the open file:<\/p>\n<p><code>r = write( fdes, text, sizeof(text)-1 );<\/code><\/p>\n<p>Immediately after, an <em>if<\/em> test confirms that variable <code>r<\/code> is equal to the size of the string written. If not, a file-writing or creation error occurred. Otherwise, the file is closed at Line 29.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre class=\"screen\">\r\n$ clang -Wall 0404.c\r\n$ .\/a.out\r\nData written to file_output.txt\r\n$ cat file_output.txt \r\nWhat a lovely little file<\/pre>\n<p>If you compile this code in a Mac terminal window, you may see a <code>permission denied<\/code> error when you attempt to view the file. For some reason, under MacOS 10.15.<em>x<\/em>, security settings fly off the wall and lock down the new file. (Actually, <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4125\">click here<\/a> to read the real reason why no permissions are set.) You can regain access by issuing the command <code>chmod +rw file_output.txt<\/code>. I have no idea why OS X is stubborn in this manner, and I have no further documentation on the topic.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4068\">Next week<\/a> I cover an interesting function that crosses between the <em>open()<\/em> and <em>fopen()<\/em> functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The companion to the raw-data reading function <em>read()<\/em> is &mdash; you guessed it &mdash; <em>write()<\/em> <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4052\">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-4052","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\/4052","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=4052"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4052\/revisions"}],"predecessor-version":[{"id":4145,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4052\/revisions\/4145"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}