Thread: Example of mkstemp

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Example of mkstemp

    Would anyone be able to create or link to a good example of mkstemp. I need to use it for an assignment and I have looked around for examples but have yet to find a good one. I just need to see how to create the file and then write to it.

    Thanks for any help,
    Dan

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Are there any that show an example of how to write to the file though?

    Dan

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Taking the example code from the man page,
    Code:
    char sfn[15] = "";
    FILE *sfp;
    int fd = -1;
    
    strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
    if ((fd = mkstemp(sfn)) == -1 ||
       (sfp = fdopen(fd, "w+")) == NULL) {
          if (fd != -1) {
            unlink(sfn);
            close(fd);
          }
       fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
       return (NULL);
    }
    return (sfp);
    it seems to me that after this does its job, you can treat sfp like a normal stream. Writing and reading should be easy; just like any other file.

  5. #5
    System-7
    Join Date
    Nov 2005
    Posts
    65
    oh alright

    Thanks
    Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mkstemp question!!!
    By zynnel in forum C Programming
    Replies: 2
    Last Post: 03-26-2003, 09:28 PM