{"id":7720,"date":"2026-08-15T00:01:06","date_gmt":"2026-08-15T07:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7720"},"modified":"2026-08-08T11:02:23","modified_gmt":"2026-08-08T18:02:23","slug":"duping-a-file-in-the-raw","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7720","title":{"rendered":"Duping a File in the Raw"},"content":{"rendered":"<p>One of C library functions I find most odd is <em>dup()<\/em>. I pronounce this function as if it rhymes with <em>cup<\/em>. I assume others may pronounce it like <em>doop<\/em>, which rhymes with <em>poop<\/em>. Whatever. The key is what the function does and how it could even remotely be necessary.<br \/>\n<!--more--><br \/>\nThe <em>dup()<\/em> function works with the file descriptors returned when you use the &#8220;raw&#8221; <em>open()<\/em> function to work with file data, as reviewed in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7711\">last week&#8217;s Lesson<\/a>. The function duplicates a file descriptor, not the value but the reference to the open file. The result is that your code has two file descriptors, which seems weird but there is a point to it all, which I eventually get around to disclosing. In the meantime, here is the <em>man<\/em> page format:<\/p>\n<p><code>int dup(int <em>oldfd<\/em>);<\/code><\/p>\n<p>The <em>oldfd<\/em> argument represents an open file descriptor. The return value is a new file descriptor, assigned the lowest integer value available. The value -1 is returned should some sorta error happen.<\/p>\n<p>The following code uses the <em>dup()<\/em> function to create two file descriptors, both of which access the same file:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_08_15-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_08_15-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;fcntl.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char filename[] = \"test.txt\";\r\n    int fd1,fd2;\r\n\r\n    <span class=\"comments\">\/* open\/create the file *\/<\/span>\r\n    fd1 = creat(filename,0);\r\n    if( fd1==-1 )\r\n    {\r\n        fprintf(stderr,\"Unable to create %s\\n\",filename);\r\n        perror(\"ERROR: \");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* duplicate the file descriptor *\/<\/span>\r\n    fd2 = dup(fd1);\r\n    if( fd2==-1 )\r\n    {\r\n        fprintf(stderr,\"Unable to duplicate the file handle\\n\");\r\n        perror(\"ERROR: \");\r\n        close(fd1);\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* write data to the file *\/<\/span>\r\n    write(fd1,\"Hello, \",7);\r\n    write(fd2,\"world!\\n\",8);\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    close(fd2);\r\n    close(fd1);\r\n    return 0;\r\n}<\/pre>\n<p>In this code, the <em>creat()<\/em> function opens\/creates a file for writing only, truncating the file if it already exists:<\/p>\n<p><code>fd1 = creat(filename,0);<\/code><\/p>\n<p>Integer variable <code>fd1<\/code> holds the <em>creat()<\/em> function&#8217;s return value, which is immediately tested for success. If the file was opened, the <em>dup()<\/em> function creates a duplicate file handle, saving it in <em>int<\/em> variable <code>fd2<\/code>:<\/p>\n<p><code>fd2 = dup(fd1);<\/code><\/p>\n<p>The function&#8217;s return value (<code>fd2<\/code>) is checked for an error. If no error is found, both file descriptors are used to write data to the file. First, <code>fd1<\/code> is used:<\/p>\n<p><code>write(fd1,\"Hello, \",7);<\/code><\/p>\n<p>Seven bytes are written to the file. The bytes represent the string <code>\"Hello, \"<\/code> plus one for the null character. Next, <code>fd2<\/code> is used in the <em>write()<\/em> function to append text to the same file:<\/p>\n<p><code>write(fd2,\"world!\\n\",8);<\/code><\/p>\n<p>As each file descriptor is open, two <em>close()<\/em> statements are necessary to wrap up the operation:<\/p>\n<p><code>close(fd2);<br \/>\nclose(fd1);<\/code><\/p>\n<p>The program generates no output, but the <em>test.txt<\/em> file created has these contents:<\/p>\n<p><code>Hello, world!<\/code><\/p>\n<p>Though two file descriptors are used, the <em>write()<\/em> function sequentially sends the string data to the same file.<\/p>\n<p>In next week&#8217;s Lesson, I continue to mess around with the duplicate file descriptors. Yes, there is a point to all this duplicate file descriptor nonsense, which I shall get to eventually.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What happens when you have two file handles referencing the same file? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7720\">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-7720","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\/7720","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=7720"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7720\/revisions"}],"predecessor-version":[{"id":7732,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7720\/revisions\/7732"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}