• C

error while using mkstemp() in loop!

Hello group,

I need to make a tmp file per each interation in a loop due to calling an external tool and storing its return values in a CSV file so I'm using:

 if((fd = mkstemp(outfile))< 0)
    {
      fprintf(stderr, "\nFailed to create temporary file to write tmp output!\n");
      exit(1);
    }

however, it works only for the 1st iteration.  For the 2nd itermation it prints the above message and quits.

Is there any limitation for this function?

Your help is appreciated.

Regards.
akohanAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
what does
fprintf(stderr, "\nFailed to create temporary file to write tmp output! outfile=%s, errno = %d:%s\n",outfile,errno,strerror(errno));
print?

you may want to reset
strcpy(outfile,"/tmp/tmp_XXXXXX") in the loop
before calling mkstemp
0
 
ozoCommented:
dis you properly format the outfile template for the 2nd itermation?
0
 
ozoCommented:
what was the errno?
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
akohanAuthor Commented:

2nd interation has the must execute the same line. Anyway, this is what I have in code:


  char outfile[] = "/tmp/tmp_XXXXXX";

Is that what you are asking?!

Thanks.
0
 
akohanAuthor Commented:

It returns -1 which is error.
0
 
ozoCommented:
what does
fprintf(stderr, "\nFailed to create temporary file to write tmp output! errno = %d :%s\n",errno,strerror(errno));
print?

you may want to reset
strcpy(outfile,"/tmp/tmp_XXXXXX") in the loop
0
 
akohanAuthor Commented:


  It prints:

  Failed to create temporary file to write tmp output! errno = 22 : Invalid argument
0
 
akohanAuthor Commented:

Thank you! Ok, I see what is happening it seems before 2nd itemration starts I have to reset the file name it to its pattern like "tmp_XXXXXX". Right?
If so, how can I do that?

Regards.
0
 
Infinity08Commented:
>> I have to reset the file name it to its pattern like "tmp_XXXXXX". Right?

mkstemp modifies the template, so yes, you have to reset it on each iteration.
0
 
akohanAuthor Commented:

Thanks got it. I'm using

strcpy(outfile, "/tmp/tmp_XXXXXX");  to clean the array. Any suggestions?

Regards.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.