Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, <code>HANDLE</code>s are completely different beasts from <code>FILE*</code>s and file descriptors. The CRT ultimately handles files in terms of <code>HANDLE</code>s and associates those <code>HANDLE</code>s to a file descriptor. Those file descriptors in turn backs the structure pointer by <code>FILE*</code>.</p> <p>Fortunately, there is a section on <a href="http://msdn.microsoft.com/en-US/library/kdfaxaay.aspx" rel="noreferrer">this MSDN page</a> that describes functions that "provide a way to change the representation of the file between a <strong>FILE</strong> structure, a file descriptor, and a Win32 file handle":</p> <blockquote> <ul> <li><code>_fdopen</code>, <code>_wfdopen</code>: Associates a stream with a file that was previously opened for low-level I/O and returns a pointer to the open stream.</li> <li><code>_fileno</code>: Gets the file descriptor associated with a stream.</li> <li><code>_get_osfhandle</code>: Return operating-system file handle associated with existing C run-time file descriptor</li> <li><code>_open_osfhandle</code>: Associates C run-time file descriptor with an existing operating-system file handle.</li> </ul> </blockquote> <p>Looks like what you need is <a href="http://msdn.microsoft.com/en-US/library/bdts1c9x.aspx" rel="noreferrer"><code>_open_osfhandle</code></a> followed by <a href="http://msdn.microsoft.com/en-US/library/dye30d82.aspx" rel="noreferrer"><code>_fdopen</code></a> to obtain a <code>FILE*</code> from a <code>HANDLE</code>.</p> <p>Here's an example involving <code>HANDLE</code>s obtained from <code>CreateFile()</code>. When I tested it, it shows the first 255 characters of the file "test.txt" and appends " --- Hello World! --- " at the end of the file:</p> <pre><code>#include &lt;windows.h&gt; #include &lt;io.h&gt; #include &lt;fcntl.h&gt; #include &lt;cstdio&gt; int main() { HANDLE h = CreateFile("test.txt", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if(h != INVALID_HANDLE_VALUE) { int fd = _open_osfhandle((intptr_t)h, _O_APPEND | _O_RDONLY); if(fd != -1) { FILE* f = _fdopen(fd, "a+"); if(f != 0) { char rbuffer[256]; memset(rbuffer, 0, 256); fread(rbuffer, 1, 255, f); printf("read: %s\n", rbuffer); fseek(f, 0, SEEK_CUR); // Switch from read to write const char* wbuffer = " --- Hello World! --- \n"; fwrite(wbuffer, 1, strlen(wbuffer), f); fclose(f); // Also calls _close() } else { _close(fd); // Also calls CloseHandle() } } else { CloseHandle(h); } } } </code></pre> <p>This should work for pipes as well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload