{"id":3175,"date":"2018-07-07T00:01:09","date_gmt":"2018-07-07T07:01:09","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3175"},"modified":"2018-06-30T10:37:44","modified_gmt":"2018-06-30T17:37:44","slug":"made-up-file-names","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3175","title":{"rendered":"Made-Up File Names"},"content":{"rendered":"<p>Many moons ago, I coded a file-renaming utility. It allows me to shuffle chapter names for documents used in my books, renaming a whole batch at once without overwriting existing filenames. It couldn&#8217;t happen if I didn&#8217;t know about the <em>mktemp()<\/em> function.<br \/>\n<!--more--><br \/>\nDefined in the <code>unistd.h<\/code> header file, the <em>mktemp(<\/em>) function generates a temporary filename. Technically, it generates a string, one that&#8217;s unique but based on a pattern or template you provide. Here&#8217;s how I use it in my file-renaming utility:<\/p>\n<p><code>t = mktemp(\"gbXXXXXX\");<\/code><\/p>\n<p>The immediate string constant <code>\"gbXXXXXX\"<\/code> provides the filename template. The X&#8217;s in the string are replaced by random characters. The resulting string is saved in <em>char<\/em> pointer variable <code>t<\/code>. The code moves on to use that temporary filename.<\/p>\n<p>Here&#8217;s a sample program:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    char template[] = \"tempXXXX\";\r\n    char *tempname;\r\n\r\n    tempname = mktemp(template);\r\n    printf(\"Temporary filename is '%s' or '%s'\\n\",\r\n            tempname,template);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Sample run:<\/p>\n<pre><code>Temporary filename is 'tempmCcZ' or 'tempmCcZ'<\/code><\/pre>\n<p>When you run it again, you get different output:<\/p>\n<pre><code>Temporary filename is 'tempzGUA' or 'tempzGUA'<\/code><\/pre>\n<p>In the code, the <em>mktemp()<\/em> function takes the template, <code>tempXXXX<\/code> and replaces the uppercase X characters with other, random characters. The result is returned as referenced in the <code>tempname<\/code> pointer, but the buffer itself is also changed, as shown in the output.<\/p>\n<p>Because the <em>mktemp()<\/em> function overwrites the template, it creates an interesting problem when you must generate more than one temporary filename based on the same pattern. To resolve this issue, you retain the template by copying it into a temporary buffer, such as <code>temp[]<\/code> used in this example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(void)\r\n{\r\n    char temp[9];\r\n    char template[] = \"tempXXXX\";\r\n    char *t;\r\n    int x;\r\n\r\n    for(x=0;x&lt;10;x++)\r\n    {\r\n        strcpy(temp,template);\r\n        t = mktemp(temp);\r\n        puts(t);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>strcpy()<\/em> function at Line 14 copies the <code>template<\/code> into the <code>temp<\/code> buffer. Then <em>mktemp()<\/em> at Line 15 uses that buffer to generate the temporary filename. Here&#8217;s the output:<\/p>\n<pre><code>tempAnWr\r\ntempdVoi\r\ntempGPwb\r\ntempvFm8\r\ntemp8Pkl\r\ntemp2scJ\r\ntemp9cFf\r\ntempoOcn\r\ntempyher\r\ntemphAwd<\/code><\/pre>\n<p>These filenames are available for use in your code, guaranteed by the function not to exist. Variations on the <em>mktemp()<\/em> function take the action further and actually create the temporary file &mdash; or even a directory. You can read more details in the <em>man<\/em> pages.<\/p>\n<p>One more point! The <em>mktemp()<\/em> function returns the <code>NULL<\/code> pointer when it&#8217;s unable to do its job. For the code you plan on running in the wild, ensure that you check for <code>NULL<\/code> after the <em>mktemp()<\/em> function returns its value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you tired of using <code>foo<\/code> and <code>temp<\/code> as temporary filenames? The C library has a solution for you. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3175\">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-3175","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\/3175","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=3175"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3175\/revisions"}],"predecessor-version":[{"id":3186,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3175\/revisions\/3186"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}