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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:14:57+00:00 2026-05-31T00:14:57+00:00

The mkstemp() function generates a unique temporary filename from template, creates and opens the

  • 0

The mkstemp() function generates a unique temporary filename from template, creates and opens the file, and returns an open file descriptor for the file. The last six characters of template must be “XXXXXX” and these are replaced with a string that makes the filename unique. Since it will be modified, template must not be a string constant, but should be declared as a character array.

After template is replaced with a string that makes the filename unique, I save the string to later use. This is where I’m encountering a strange problem I can’t seem to wrap my head around. I can print the correct filename to my terminal, see the file in my file explorer and open it to see the correct contents, but when I include the string as part of a command to execute with popen() I get a pointer to an empty file. When I hard code the names of the tempory files back into my code however and run again, I get the correct result I am expecting. Is there something I’m overlooking or missing? Here is a code snippet:

char tmpname[] = "tmp.XXXXXX";
FILE *fpt = fdopen(mkstemp(tmpname), "w");
string saved_tmpname(tmpname);
// blah
// write to file
// blah blah
const string command = "mycommand " + saved_tmpname;
cout << command << endl; // prints correctly
FILE *fpipe = popen(command.c_str(), "r");
if (fpipe == NULL) {
  perror(command.c_str());
}
char buff[4096];
while (fgets(buff, 4096, fpipe)) {
  // we don't get here!
}
  • 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-31T00:14:59+00:00Added an answer on May 31, 2026 at 12:14 am

    From the manpage for mkstemp:

    The file is opened with the open(2) O_EXCL flag, guaranteeing that the caller is the process that creates the file.

    The O_EXCL flag prevents you from opening the file again. This is OK, since it is a temporary file – only one process (the creator) should have access to it. Temporary files contain sensitive data sometimes.

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

Sidebar

Related Questions

In Python I open a temporary file for write by using tempfile.mkstemp in order
In node.js, how do I generate a unique temporary file name, a la mkstemp(3)
tempfile.mkstemp() returns: a tuple containing an OS-level handle to an open file (as would
I'm playing around with mkstemp() , which provides a file descriptor, but I want
Both operations create an empty file and return the filename but mkstemp leaves the
I'd like to create a unique temporary directory in Windows from some C code
I am transitioning a program that uses temporary files from POSIX FILE to C++
I have created a temporary file. Added some data to the file created. Saved
I tried to use the read/write file descriptor in bash so that I could
Okay, mkstemp is the preferred way to create a temp file in POSIX. But

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.