用mkstemp创建临时文件。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main( int argc, char *argv[] )
{
char temp[45] = "wangkaiXXXXXX";
int fd;
if(( fd = mkstemp( temp ) ) < 0){
printf( "mkstemp error !\n" );
perror("mktemp ");
}
printf( "temp : %s\n",temp );
close(fd);
exit(0);
}
在创建的文件必须要手动unlink。否则的,该文件不会自动删除。
加上unlink后,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main( int argc, char *argv[] )
{
char temp[45] = "wangkaiXXXXXX";
int fd;
if(( fd = mkstemp( temp ) ) < 0){
printf( "mkstemp error !\n" );
perror("mktemp ");
}
printf( "temp : %s\n",temp );
unlink(temp);
close(fd);
exit(0);
}