{"id":110026,"date":"2024-07-22T07:00:00","date_gmt":"2024-07-22T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=110026"},"modified":"2024-07-22T04:53:57","modified_gmt":"2024-07-22T11:53:57","slug":"20240722-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240722-00\/?p=110026","title":{"rendered":"Organizing the five creation dispositions of the <CODE>Create&shy;File<\/CODE> function"},"content":{"rendered":"<p>The <code>Create\u00adFile<\/code> function has five different creation disposition values. Let&#8217;s organize them.<\/p>\n<p>If a file of the requested name doesn&#8217;t already exist, you have two choices: You can fail the operation, or you can create a new file.<\/p>\n<p>If a file of the requested name already exists, you have <i>three<\/i> choices: You can fail the operation, you can open the existing file and preserve the existing data, or you can open the existing file but set the file length back to zero (truncate), discarding any existing file contents.<\/p>\n<p>Now that we see the possibilities, we can make our table.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse; text-align: center;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th rowspan=\"2\">If file exists<\/th>\n<th colspan=\"2\">If file doesn&#8217;t exist<\/th>\n<\/tr>\n<tr>\n<th>Fail<\/th>\n<th>Create<\/th>\n<\/tr>\n<tr>\n<th>Fail<\/th>\n<td>&nbsp;<\/td>\n<td><code>CREATE_NEW<\/code><\/td>\n<\/tr>\n<tr>\n<th>Open<\/th>\n<td><code>OPEN_EXISTING<\/code><\/td>\n<td><code>OPEN_ALWAYS\n<\/code><\/td>\n<\/tr>\n<tr>\n<th>Truncate<\/th>\n<td><code>TRUNCATE_EXISTING<\/code><\/td>\n<td><code>CREATE_ALWAYS<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8220;Hey, wait, I found a hole in your API. You missed a case: The upper left box is empty!&#8221;<\/p>\n<p>That&#8217;s right, but the upper left box is &#8220;Fail if the file exists, and fail if the file doesn&#8217;t exist.&#8221; If we had to give that box a name, it would be something like <code>FAIL_ALWAYS<\/code>.<\/p>\n<p>Which isn&#8217;t particularly useful.<\/p>\n<p>If it&#8217;s that important to you, I guess you can simulate it yourself:<\/p>\n<pre>HANDLE CreateFileFailAlways()\r\n{\r\n    SetLastError(ERROR_OPEN_FAILED);\r\n    return INVALID_HANDLE_VALUE;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Six possibilities, but only five useful ones.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-110026","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Six possibilities, but only five useful ones.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=110026"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110026\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=110026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=110026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=110026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}