Thread: mkstemp inside a loop

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    5

    Talking mkstemp inside a loop

    Hi folks,
    i'm writting a piece of code, and i need to write data to temporary files created by mkstemp function.
    The problem occurs at the second and later calls.
    Here is the strace output of a sample.c :

    sample.c
    Code:
    #include <stdio.h>
    char tn[]="/tmp/TEST-XXXXXX";
    int main(int argc, char *argv[]){
     int i,tmpfd;
            for(i=0;i<5;i++) {
                    tmpfd=mkstemp(tn);
                    write(tmpfd,"HI\n",3);
                    close(tmpfd);
            }
            return 0;
    }
    The strace output:
    Code:
    open("/tmp/TEST-uvSflv", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
    write(3, "OI\n", 3)   = 3
    close(3)                = 0
    write(-1, "OI\n", 3) = -1 EBADF (Bad file descriptor)
    close(-1)               = -1 EBADF (Bad file descriptor)
    write(-1, "OI\n", 3)  = -1 EBADF (Bad file descriptor)
    close(-1)                = -1 EBADF (Bad file descriptor)
    write(-1, "OI\n", 3)   = -1 EBADF (Bad file descriptor)
    close(-1)                 = -1 EBADF (Bad file descriptor)
    write(-1, "OI\n", 3)    = -1 EBADF (Bad file descriptor)
    close(-1)                 = -1 EBADF (Bad file descriptor)
    exit_group(0)           = ?
    So the mkstemp fail to open a new fd every new call inside a loop.
    Well, according to the manpage the mkstemp function create and open the file, so we expect a new file (fd) every time right?

    somebody know why this happen and how fix this ?
    thanks a lot!
    Last edited by jsh; 12-27-2011 at 01:41 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to reset your template string to have the 6 X's in it.

    http://pubs.opengroup.org/onlinepubs...s/mkstemp.html
    The mkstemp() function shall replace the contents of the string pointed to by template by a unique filename
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    ha!
    Thank you very much salem!
    It was fast!! And solve the case

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  2. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 01:19 AM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. Example of mkstemp
    By Dan17 in forum C Programming
    Replies: 4
    Last Post: 09-13-2006, 07:20 PM
  5. mkstemp question!!!
    By zynnel in forum C Programming
    Replies: 2
    Last Post: 03-26-2003, 09:28 PM