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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:34:39+00:00 2026-06-16T01:34:39+00:00

I need to create a temporary file in my C program, write some data

  • 0

I need to create a temporary file in my C program, write some data to it, and then invoke an external command (via exec or system) to do some processing on the file I just created. I did not write the external command nor is it feasible to integrate it into my program so I don’t think I can share an already open descriptor with it. Therefore, I need to know the name of the temp file created.

The tempname() function does this, but unfortunately it recommends that you don’t use itself, due to a possible race condition between getting the name and opening the file, and neither of the functions it recommends (tmpfile and mkstemp) provide a way to find out the actual name of the file created.

  • 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-16T01:34:41+00:00Added an answer on June 16, 2026 at 1:34 am

    It is not true that mkstemp does not let you know the temporary file name, try to compile and execute this program to see yourself:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {   
        char fn[] = "/tmp/fileXXXXXX";
        int fd = mkstemp(fn);
        char cmd[200];
        int n = snprintf(cmd, sizeof(cmd), "ls -l %s\n", fn);
    
        printf("snprintf=>%d\n sizeof(fn)=%d\n", n, sizeof(fn)); // extra info, see comments
    
        printf("%s\n", cmd);
        return system(cmd);
    } 
    

    mkstemp will replace the file name template in the buffer you pass to it with actual file name, you can do whatever you want with this buffer later on.

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

Sidebar

Related Questions

I need to write some data in a temporary file and to store this
I know you can create a temporary file with tmpfile and than write to
I need to create startup and stop scripts for a python program. My temporary
I am writing a C++ program which needs to create a temporary file for
I need to create a temporary file to store user settings on the client
I need to create a temporary file to send it, I have tried :
I need to write text file from textbox on button's click event after writing
I need to create temporary files with PHP for my site. How can I
I want to be able to create, rename or remove the temporary file inside
Currently I have some code deleting some temporary files created by my program: #

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.