本文介绍了对于 HANDLE,是否有与 fdopen 等效的 Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Unix 中,如果您有一个文件描述符(例如来自套接字、管道或从您的父进程继承),您可以使用 FILE* 在其上打开一个缓冲的 I/O FILE* 流a href="http://linux.die.net/man/3/fdopen">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).

Windows 上是否有 HANDLE 的等效项?如果您有一个从父进程继承的 HANDLE(不同于 stdin、stdout 或 stderr)或来自 CreatePipe 的管道,是否有可能获得一个缓冲的 FILE* 来自它的流?MSDN 确实记录了 _fdopen,但这适用于 _open 返回的整数文件描述符,而不是通用的 HANDLE s.

Is there an equivalent on Windows for HANDLEs? 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 HANDLEs.

推荐答案

不幸的是,HANDLEFILE* 和文件描述符完全不同.CRT 最终根据 HANDLE 处理文件,并将这些 HANDLE 与文件描述符相关联.这些文件描述符依次通过 FILE* 返回结构指针.

Unfortunately, HANDLEs are completely different beasts from FILE*s and file descriptors. The CRT ultimately handles files in terms of HANDLEs and associates those HANDLEs to a file descriptor. Those file descriptors in turn backs the structure pointer by FILE*.

幸运的是,此 MSDN 页面 上有一个部分描述函数提供了一种在 FILE 结构、文件描述符和 Win32 文件句柄之间更改文件表示的方法":

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:将一个流与一个文件关联起来先前为低级 I/O 打开并返回指向打开的指针流.
  • _fileno:获取与流关联的文件描述符.
  • _get_osfhandle:返回相关的操作系统文件句柄使用现有的 C 运行时文件描述符
  • _open_osfhandle:将 C 运行时文件描述符与现有的操作系​​统文件句柄.

看起来你需要的是_open_osfhandle 之后是 _fdopen 以获得一个FILE* 来自 HANDLE.

Looks like what you need is _open_osfhandle followed by _fdopen to obtain a FILE* from a HANDLE.

这是一个涉及从 CreateFile() 获得的 HANDLE 的示例.当我测试它时,它显示文件test.txt"的前255个字符并在文件末尾附加--- Hello World!---":

Here's an example involving HANDLEs 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
", rbuffer);
                fseek(f, 0, SEEK_CUR); // Switch from read to write
                const char* wbuffer = " --- Hello World! ---
";
                fwrite(wbuffer, 1, strlen(wbuffer), f);
                fclose(f); // Also calls _close()
            }
            else
            {
                _close(fd); // Also calls CloseHandle()
            }
        }
        else
        {
            CloseHandle(h);
        }
    }
}

这也适用于管道.

这篇关于对于 HANDLE,是否有与 fdopen 等效的 Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:50