[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

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.
0
akohan
Asked:
akohan
  • 5
  • 4
1 Solution
 
ozoCommented:
dis you properly format the outfile template for the 2nd itermation?
0
 
ozoCommented:
what was the errno?
0
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
ozoCommented:
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now