O_CREAT 函数已经用过了,若不存在就新建一个文件
我们来看看 O_EXAL 函数吧(若文件存在,则返回-1)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<stdio.h>
int main()
{
int fd;
fd=open("./qqq.c",O_RDWR|O_CREAT|O_EXCL,0600);
if(fd==-1)
{
printf("file exist!\n");
}
else
{
printf("creat file!\n");
}
close(fd);
return 0;
}