打开/创建文件
open 文件会返回一个文件描述符 (索引)
参数说明
Pathname:要打开的文件名(含路径,缺省为当前路径)
Flags: O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开
当我们附带了权限后,打开的文件就只能按照这种权限来操作。 以上这三个常数中应当只指定一 个。下列常数是可选择的:
O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。
O_EXCL 如果同时指定了O_CREAT,而文件已经存在,则出错。
O_APPEND 每次写时都加到文件的尾端。
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0(把它干掉)。
Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限
创建文件creat函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "text?";
fd = creat("/home/CLC/file1",S_IRWXU);//可读可写可执行
printf("open suscees!fd = %d\n",fd);
return 0;
}
在/home/CLC/file1路径创建可读、写、执行文件file。
代码实现
O_RDWR
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;//文件描述符
fd = open("./file1",O_RDWR);
printf("fd=%d\n",fd);
return 0;
}
结果:fd = 3 (w文件打开成功返回3正数)
rm file1(删除file1文件后)结果:fd = -1 文件打开失败返回-1负数
O_CREAT
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);//O_RDWR 可读可写打开 |O_CREAT 文件不存在可创建
if(fd > 0){
printf("create file1\n");
}
}
printf("fd = %d\n",fd);
return 0;
}
结果:fd = 3
rm file1(删除file1文件后)结果:
open file1 failed
create file1
fd = 3
0600是干嘛的?
文件权限:1.可读 r 4 2.可写 w 2 3.执行 x 1
0600: 6是4+2,可读可写, 6后面的第一个0 表示同组 ,第二个表示其他组
就是给文件所有者
total 68
-rwxr-xr-x 1 CLC book 8531 Mar 3 21:10 a.out
drwxr-xr-x 3 CLC book 4096 Mar 3 15:55 chushi
-rw-r--r-- 1 CLC book 176 Mar 3 19:12 demo1.c
-rw-r--r-- 1 CLC book 320 Mar 3 19:30 demo2.c
-rw-r--r-- 1 CLC book 470 Mar 3 20:03 demo3.c
-rw-r--r-- 1 CLC book 738 Mar 3 20:31 demo4.c
-rw-r--r-- 1 CLC book 781 Mar 3 20:44 demo5.c
-rw-r--r-- 1 CLC book 815 Mar 3 21:03 demo6.c
-rw-r--r-- 1 CLC book 344 Mar 3 21:10 demo7.c
-rw------- 1 CLC book 19 Mar 3 21:03 file1
drwxr-xr-x 2 CLC book 4096 Mar 1 20:45 kongZhi
drwxr-xr-x 2 CLC book 4096 Feb 22 08:44 long
drwxr-xr-x 2 CLC book 4096 Mar 3 17:00 shuzu
-rw-r--r-- 1 CLC book 1044 Jan 31 2021 snakemap.c~
drwxr-xr-x 2 CLC book 4096 Feb 21 09:42 zhizhen
O_EXCL
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("./file1",O_RDWR);
if(fd == -1){
fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);//可读可写 | 不存在创建 | 文件存在出错
printf("file cunZai\n");
}
return 0;
}
file文件存在时,由于O_EXCL出错不打印后面内容;
file文件删除后,执行程序后打印file cunzai ,并创建文件file。
O_APPEND
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "chenglong henshuai?";
fd = open("./file1",O_RDWR|O_APPEND);
printf("fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
return 0;
}
j结果:在file1 文件添加12345678,在执行代码后file1文件12345678另起一行添加chenglong henshuai?
O_TRUNC
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "text?";
fd = open("./file1",O_RDWR|O_TRUNC);//将file1文件里面原有的东西干掉
printf("open suscees!fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));//在file文件写入text?
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
return 0;
}