boom
boom

Reputation: 6166

creating a temporary folder in tmp folder c language Mac OS X

How can i create a temporary folder in /tmp directory.

Upvotes: 1

Views: 2744

Answers (2)

Tim Kane
Tim Kane

Reputation: 2779

Try the mkdtemp function.

char *tmpdir;
strcpy (template, "/tmp/myprog.XXXXXX");
tmpdir = mkdtemp (template);

if (!tmpdir) {
    // Error out here
}

printf ("Temporary directory created : %s", tmpdir);

Upvotes: 2

jweyrich
jweyrich

Reputation: 32260

Since I can't change/improve other's answers yet, I'm writing one myself.

I'd use stat and mkdir. For example:

#include <errno.h> // for errno
#include <stdio.h> // for printf
#include <stdlib.h> // for EXIT_*
#include <string.h> // for strerror
#include <sys/stat.h> // for stat and mkdir

int main() {
    const char *mydir = "/tmp/mydir";
    struct stat st;
    if (stat(mydir, &st) == 0) {
        printf("%s already exists\n", mydir);
        return EXIT_SUCCESS;
    }
    if (mkdir(mydir, S_IRWXU|S_IRWXG) != 0) {
        printf("Error creating directory: %s\n", strerror(errno));
        return EXIT_FAILURE;
    }
    printf("%s successfully created\n", mydir);
    return EXIT_SUCCESS;
}

Upvotes: 0

Related Questions