mkstemp
#include <stdlib.h>
int mkstemp(char *template);
实例:
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/epoll.h>
#include <sys/uio.h>
#include <limits.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
static char *file_name = "writev.txt";
int main(int argc, char **argv)
{
int fd ;
char name[] = "/tmp/lkmao_XXXXXX";
fd = mkstemp(name);
if(fd < 0){
perror("mkstemp");
return -1;
}
DEBUG_INFO("mkstemp %s ok",name);
unlink(name);
close(fd);
return 0;
}
FILE *tmpfile(void);
#include <stdio.h>
FILE *tmpfile(void);
#include <stdio.h>
int fileno(FILE *stream);
测试代码:
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/epoll.h>
#include <sys/uio.h>
#include <limits.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
static char *file_name = "writev.txt";
int main(int argc, char **argv)
{
FILE* p = tmpfile();
if(p == NULL){
perror("tmpfile");
return -1;
}
int fd = fileno(p);
if(fd < 0){
perror("fileno");
return -1;
}
off_t offset = 0;
pwrite(fd,"hello tmp",sizeof("hello tmp"),offset);
char buf[100];
int len = pread(fd,buf,sizeof(buf),offset);
buf[len] = '\0';
DEBUG_INFO("buf = %s",buf);
DEBUG_INFO("after pread:offset = %ld",ftell(p));
len = read(fd,buf,sizeof(buf));
buf[len] = '\0';
DEBUG_INFO("buf = %s",buf);
DEBUG_INFO("after read:offset = %ld",ftell(p));
fclose(p);
return 0;
}
测试结果:
小结