Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6546817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:45:18+00:00 2026-05-25T11:45:18+00:00

In Unix, if you have a file descriptor (e.g. from a socket, pipe, or

  • 0

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).

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:45:19+00:00Added an answer on May 25, 2026 at 11:45 am

    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*.

    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: 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.

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

    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\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);
            }
        }
    }
    

    This should work for pipes as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following file (above) which seems to be an Unix pipe alt
Is there a unix tool to which I can pass a file and have
I have a question in mind. By convention, unix associates the file descriptor 0,
I have a file we will call info.txt under UNIX format that has only
I have a CSV file with several entries, and each entry has 2 unix
I have a problem. I have a batch file which imitates the UNIX cd
I have a standard passwd file & a usermap file - which maps unix
How to create a .so and .a file in UNIX. Do we have any
I have two (UNIX) programs A and B that read and write from stdin/stdout.
I have a file containing a list of unix commands like ls -la /etc/passwd.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.