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

  • SEARCH
  • Home
  • 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 121865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:02:17+00:00 2026-05-11T04:02:17+00:00

Okay, mkstemp is the preferred way to create a temp file in POSIX. But

  • 0

Okay, mkstemp is the preferred way to create a temp file in POSIX.

But it opens the file and returns an int, which is a file descriptor. From that I can only create a FILE*, but not an std::ofstream, which I would prefer in C++. (Apparently, on AIX and some other systems, you can create an std::ofstream from a file descriptor, but my compiler complains when I try that.)

I know I could get a temp file name with tmpnam and then open my own ofstream with it, but that’s apparently unsafe due to race conditions, and results in a compiler warning (g++ v3.4. on Linux):

warning: the use of `tmpnam' is dangerous, better use `mkstemp' 

So, is there any portable way to create an std::ofstream to a temp file?

  • 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. 2026-05-11T04:02:18+00:00Added an answer on May 11, 2026 at 4:02 am

    I think this should work:

        char *tmpname = strdup('/tmp/tmpfileXXXXXX');     ofstream f;     int fd = mkstemp(tmpname);     f.attach(fd); 

    EDIT: Well, this might not be portable. If you can’t use attach and can’t create a ofstream directly from a file descriptor, then you have to do this:

    char *tmpname = strdup('/tmp/tmpfileXXXXXX'); mkstemp(tmpname); ofstream f(tmpname); 

    As mkstemp already creates the file for you, race condition should not be a problem here.

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

Sidebar

Related Questions

Okay, I feel a bit foolish for having to ask this but I guess
Okay, I have the following create action #posts_controller, nested resource under discussions def create
Okay, this is probably a very basic question; but, I'm just getting back in
Okay suppose I have an array of objects that look like this: obj(from, to)
Okay I'm trying to move from VB to C# with mixed success. I'm getting
Okay, so the answer to my question might not be the problem but here's
Okay, I've set up a bit of code which searching for all the pages
OKay, this is probably pretty noob, but I couldn't find how to solve it.
Okay, I am using a custom tab bar (not a tab based app) but
Okay, I haven't used Interface Builder since Xcode 3.x but I remember there being

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.