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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:23:05+00:00 2026-06-14T06:23:05+00:00

I have to duplicate a FILE* in C on Mac OS X (using POSIX

  • 0

I have to duplicate a FILE* in C on Mac OS X (using POSIX int file descriptors all the way is unfortunately out of question), so I came up with the following function:

static FILE* fdup(FILE* fp, const char* mode)
{
    int fd = fileno(fp);
    int duplicated = dup(fd);
    return fdopen(duplicated, mode);
}

It works very well, except it has that small ugly part where I ask for the file mode again, because fdopen apparently can’t determine it itself.

This issue isn’t critical, since basically, I’m just using it for stdin, stdout and stderr (and obviously I know the access modes of those three). However, it would be more elegant if I didn’t have to know it myself; and this is probably possible since the dup call doesn’t need it.

How can I determine the access mode of a FILE* stream?

  • 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-06-14T06:23:07+00:00Added an answer on June 14, 2026 at 6:23 am

    You can’t, but you can determine the mode for the underlying file descriptor:

    int fd = fileno(f);
    int accmode = fcntl(fd, F_GETFL) & O_ACCMODE;
    

    You can then choose an appropriate mode to pass to fdopen based on whether accmode is O_RDONLY, O_WRONLY, or O_RDWR.

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

Sidebar

Related Questions

Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
I have the following file dupeExtracter.rb to remove duplicate entries from a DB table
I have duplicate entries in my log file for NServiceBus and I'm concerned this
I have to remove duplicate strings from extremely big text file (100 Gb+) Since
Possible Duplicate: Problem executing bash file I have created a bash file script and
Possible Duplicate: Prepend lines to file in Java I have a CSV-file which supports
Possible Duplicate: How to check file types of uploaded files in PHP? I have
Possible Duplicate: Check synchronously if file/directory exists in Node.js For example, i have a
Possible Duplicate: How to include js file in another js file? Suppose I have
I have a loop that finds duplicate lines in a .ini file. I can

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.