是否有一个Windows等同于fdopen的手柄? [英] Is there a Windows equivalent to fdopen for HANDLEs?
问题描述
在Unix上,如果有一个文件描述符(从插座,管道例如,或者从你的父进程继承),你可以打开一个缓冲I / O FILE *
流它与 fdopen(3)
。
In Unix, if you have a file descriptor (e.g. from a socket, pipe, or inherited from your parent process), you can open a buffered I/O FILE*
stream on it with fdopen(3)
.
是否有 HANDLE
S于Windows中的相同呢?如果你有一个 HANDLE
这是从你的父进程(从标准输入,标准输出,或标准错误不同),或从 CreatePipe $ C管道继承$ C>,是有可能得到一个缓冲
秒。 FILE *
流? MSDN文档是否 _fdopen
,但适用于>按 _open
HANDLE
Is there an equivalent on Windows for HANDLE
s? If you have a HANDLE
that was inherited from your parent process (different from stdin, stdout, or stderr) or a pipe from CreatePipe
, is it possible to get a buffered FILE*
stream from it? MSDN does document _fdopen
, but that works with integer file descriptors returned by _open
, not generic HANDLE
s.
推荐答案
不幸的是, HANDLE
是由 FILE *完全不同的野兽
S和文件描述符。 CRT的最终处理在 HANDLE
取值方面的文件和相关联的 HANDLE
s到文件描述符。在将这些文件描述符 FILE *备份结构的指针
。
Unfortunately, HANDLE
s are completely different beasts from FILE*
s and file descriptors. The CRT ultimately handles files in terms of HANDLE
s and associates those HANDLE
s to a file descriptor. Those file descriptors in turn backs the structure pointer by FILE*
.
幸运的是,在描述函数提供了这个MSDN页面方法来改变的文件结构,文件描述符,和一个Win32文件句柄的文件重新presentation:
Fortunately, there is a section on this MSDN page that describes functions that "provide a way to change the representation of the file between a FILE structure, a file descriptor, and a Win32 file handle":
-
_fdopen
,_wfdopen
:与关联起来,这是一个文件流
previously打开低级别的I / O,并返回一个指向打开
流。 -
_fileno
:获取与流相关的文件描述符 -
_get_osfhandle
:返回操作系统文件句柄相关联
与现有的C运行时文件描述符 -
_open_osfhandle
:Associates的C运行时文件与描述符
现有的操作系统文件句柄。
_fdopen
,_wfdopen
: Associates a stream with a file that was previously opened for low-level I/O and returns a pointer to the open stream._fileno
: Gets the file descriptor associated with a stream._get_osfhandle
: Return operating-system file handle associated with existing C run-time file descriptor_open_osfhandle
: Associates C run-time file descriptor with an existing operating-system file handle.
看起来像你需要的是 _open_osfhandle
后跟 _fdopen
来>获得 FILE *
从 HANDLE
。
Looks like what you need is _open_osfhandle
followed by _fdopen
to obtain a FILE*
from a HANDLE
.
下面是涉及 HANDLE一个例子
■从获得的CreateFile()
。当我测试了它,它显示的文件的test.txt的前255个字符并追加---世界您好---!在文件的结尾:
Here's an example involving HANDLE
s obtained from CreateFile()
. 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:
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <cstdio>
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);
}
}
}
这应该管正常工作。
这篇关于是否有一个Windows等同于fdopen的手柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!