Hi,
How can i create a temporary folder in /tmp directory.
Hi,
How can i create a temporary folder in /tmp directory.
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);
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;
}