{"id":98065,"date":"2018-02-21T07:00:00","date_gmt":"2018-02-21T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=98065"},"modified":"2019-03-13T01:01:25","modified_gmt":"2019-03-13T08:01:25","slug":"20180221-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180221-00\/?p=98065","title":{"rendered":"How can I call freopen but open the file with shared access instead of exclusive access?"},"content":{"rendered":"<p>A customer wants to redirect their program&#8217;s <code>stdout<\/code> to a file. They followed the <a HREF=\"https:\/\/support.microsoft.com\/en-us\/help\/58667\/how-to-redirect-stdout-to-a-file-from-a-visual-c-c-program\">sample code<\/a> which basically boils down to the line <\/p>\n<pre>\n    FILE* stream = freopen(\"output.txt\", \"w\", stdout);\n<\/pre>\n<p>or its security-enhanced alternate version: <\/p>\n<pre>\n    errno_t err = freopen_s(&amp;stream, \"output.txt\", \"w\", stdout);\n<\/pre>\n<p>The customer reported that this worked exactly as exepcted, but the output file is opened for exclusive access. They want another process to be able to read from the output file while the original process is writing to it, but the exclusive access prevents that. <\/p>\n<p>The Microsoft-specific function <code>_fsopen<\/code> lets you specify a custom sharing mode, but there is no corresponding <code>_fsreopen<\/code> function that augments the <code>freopen<\/code> function with a sharing mode. <\/p>\n<p>Is there anything the customer can do? <\/p>\n<p>The C\/C++ runtime library folks suggested using <code>_dup2<\/code> to remap the file descriptor. Something like this: <\/p>\n<pre>\n#include &lt;fcntl.h&gt;\n#include &lt;io.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;windows.h&gt;\n\n\/\/ All error checking omitted for clarity\nint main()\n{\n  \/\/ Open with desired sharing mode\n  HANDLE h = CreateFileW(L\"output.txt\", GENERIC_WRITE, FILE_SHARE_READ,\n                         nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,\n                         nullptr);\n\n  \/\/ Convert to a file descriptor\n  int fd = _open_osfhandle(reinterpret_cast&lt;intptr_t&gt;(h), _O_WRONLY);\n\n\n  \/\/ Remap stdout's file descriptor to be a copy of the one we just created\n  _dup2(fd, _fileno(stdout));\n\n  \/\/ Don't need our file descriptor any more\n  _close(fd);\n\n\n  printf(\"Hello, world!\\n\");\n\n  return 0;\n}\n<\/pre>\n<p>The customer confirmed that this does exactly what they needed. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coming through the back way.<\/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-98065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Coming through the back way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98065","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=98065"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98065\/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=98065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=98065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=98065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}