mkstemp inside a loop

This is a discussion on mkstemp inside a loop within the C Programming forums, part of the General Programming Boards category; Hi folks, i'm writting a piece of code, and i need to write data to temporary files created by mkstemp ...

  1. #1
    jsh
    jsh is offline
    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 12:41 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    jsh
    jsh is offline
    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, 08:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21