• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.