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 414163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:15:20+00:00 2026-05-12T18:15:20+00:00

How can one create a new file descriptor from an existing file descriptor such

  • 0

How can one create a new file descriptor from an existing file descriptor such that the new descriptor does not share the same internal file structure/entry in the file table? Specifically attributes such as file offset (and preferably permissions, sharing and modes) should not be shared between the new and old file descriptors.

Under both Windows and Linux, dup() will duplicate the file descriptor, but both descriptors still point to the same file structure in the process’ file table. Any seeking on either descriptor will adjust the position for the other descriptors as well.

Note

I’ve since received answers for both Windows and Linux and adjusted the question a little too often, which has made it difficult for people to answer. I’ll adjust my votes and accept the cleanest answer which covers both Windows and Linux. Apologies to all, I’m still new to the SO paradigm. Thanks for the great answers!

  • 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-12T18:15:21+00:00Added an answer on May 12, 2026 at 6:15 pm

    So basically, what you really want is to be given a file descriptor, and basically open the same file over again, to get a separate position, sharing, mode, etc. And you want to do this on Windows (where the “file descriptor” is basically a foreign object, not something used directly by the OS or the run-time library at all.

    Amazingly enough, there is a way to do that, at least with MS VC++. All but two steps of it use only the Win32 API so porting to other compilers/libraries should be fairly reasonable (I think most supply versions of those two functions). Those are for converting a Unix-style file descriptor to a native Win32 file handle, and converting a native Win32 file handle back to a Unix-style file descriptor.

    1. Convert file-descriptor to native file handle with _get_osfhandle()
    2. Get a name for the file with GetFileInformationByHandleEx(FILE_NAME_INFO)1
    3. Use CreateFile to open a new handle to that file
    4. Create a file descriptor for that handle with _open_osfhandle()

    Et voilà, we have a new file descriptor referring to the same file, but with its own permissions, position, etc.

    Toward the end of your question, you make it sound like you also want the “permissions”, but that doesn’t seem to make any real sense — the permissions attach to the file itself, not to how the file is opened, so opening or reopening the file has no effect on the file’s permissions. If you really want to know the, you can get it with GetFileInformationByHandle, but be aware that file permissions in Windows are quite a bit different from the (traditional) file permissions in Unix. Unix has owner/group/world permissions on all files, and most systems also have ACLs (though there’s more variation in how they work). Windows either has no permissions at all (e.g., files on FAT or FAT32) or else uses ACLs (e.g., files on NTFS), but nothing that’s really equivalent to the traditional owner/group/world permissions most people are accustomed to on Unix.

    Perhaps you’re using “permissions” to refer to whether the file was open for reading, writing, or both. Getting that is considerably uglier than any of the preceding. The problem is that most of it is in the library, not Win32, so there’s probably no way to do it that will be even close to portable between compilers. With MS VC++ 9.0 SP1 (not guaranteed for any other compiler) you can do this:

    #include <stdio.h>
    
    int get_perms(int fd) {
        int i;
     FILE * base = __iob_func();
    
        for (i=0; i<_IOB_ENTRIES; i++) 
            if (base[i]._file == fd)
                return base[i]._flag;     // we've found our file
        return 0; // file wasn't found.
    }
    

    Since this involved some spelunking, I wrote a quick test to verify that it might actually work:

    #ifdef TEST
    #include <io.h>
    
    void show_perms(int perms, char const *caption) { 
     printf("File opened for %s\n", caption);
     printf("Read permission = %d\n", (perms & _IOREAD)!=0);
     printf("Write permission = %d\n", (perms & _IOWRT)!=0);
    }
    
    int main(int argc, char **argv) { 
     FILE *file1, *file2;
     int perms1, perms2;
    
     file1=fopen(argv[1], "w");
     perms1 = get_perms(_fileno(file1));
     fclose(file1);
    
     file2=fopen(argv[1], "r");
     perms2 = get_perms(_fileno(file2));
     fclose(file2);
    
     show_perms(perms1, "writing");
     show_perms(perms2, "reading");
     return 0;
    }
    #endif
    

    And the results seem to indicate success:

    File opened for writing
    Read permission = 0
    Write permission = 1
    File opened for reading
    Read permission = 1
    Write permission = 0
    

    You can then test that returned flag against _IOREAD, _IOWRT, and _IORW, which are defined in stdio.h. Despite my previous warnings, I should probably point out that I suspect (though I certainly can’t guarantee) that this part of the library is fairly stable, so the real chances of major changes are probably fairly minimal.

    In the other direction, however, there’s basically no chance at all that it’ll work with any other library. It could (but certainly isn’t guaranteed to) work with the other compilers that use the MS library, such as Intel, MinGW or Comeau using MS VC++ as its back-end. Of those, I’d say the most likely to work would be Comeau, and the least likely MinGW (but that’s only a guess; there’s a good chance it won’t work with any of them).

    1. Requires the redistributable Win32 FileID API Library
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 207k
  • Answers 207k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just put a question mark after the {0,1}, as in… May 12, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer Did you check if the PATH is still there in… May 12, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer You (like me) are looking for static extension methods: http://madprops.org/blog/static-extension-methods/… May 12, 2026 at 9:27 pm

Related Questions

I'm passing a query to an internal application that runs that query and returns
A bit long, but, shorter than the first draft :) We have a small
The project I'm using uses Cons instead of Make for one section, for reasons
I am trying to create a new XML file from an exisiting one using
How can you merge two branches in git, retaining necessary files from a branch?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.