linux下open() read() write() lseek() close() 函数的用法
open()函数的用法
以下是函数的原型
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
返回值 : open函数打开文件,打开成功返回一个文件描述符,打开失败,返回-1
*pathname: 要打开的文件名(含文件路径,缺省为当前路径)
flags:O_RDONLY 只读打开
O_RWONLY 只写打开
O_RDWR 可读可写打开
当我们附带了权限后,打开的文件就只能按照这种权限来操作
以上三种常数中应当只指定一个,下列常数是可以选择的
-
O_CREAT 若文件不存在则创建他,使用此选项时,需同时说明第三个参数mode。用其说明该文件的存取许可权限
-
O_EXCL 如果同时指定了O_CREAT. 而文件存在则出错
-
O_APPEND 每次写时都加到文件的尾端;
-
O_TRUNC 属性去打开文件时,如果文件中本来就有内容,而且为只读或只写成功打开,则将其长度改为0
Mode:一定是在flag中使用了O_CREAT标志,mode记录待创建的文件的访问权限
int creat(const char *filename,mode_t mode)
filiname 要创建的文件名(包含路径,缺省为当前路径)
mode 创建模式(可读 可写 可执行)
常见创建模式
宏表示 数字
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读 可写 可执行
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
int main()
{
int fd;
fd = open("./fili1",O_RDWR);
printf("%d",fd);
return 0;
}
运行结果3 如果没有该文件返回值时-1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
int main()
{
int fd;
fd = open("./fili1",O_RDWR);
if(fd = -1)
{
printf("open filied\n");
fd = open("./fili1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
printf("sucessful\n");
}
}
printf("%d",fd);
return 0;
}
如果没有fili1 则创建fili1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
int main()
{
int fd;
fd = open("./fili1",O_RDWR);
printf("%d",fd);
return 0;
}
O_RDWR|O_CREAT|O_EXCL 文件不存在创建文件 文件存在则出错返回-1
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include<string.h>
int main()
{
char *buf = "zhaoyujian";
int fd = open("fili1",O_RDWR|O_TRUNC);
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
创建文件 文件权限可读可写可执行
ls -l 是把所有的清单列出来
-rw-r--r-- 1 CLC book 0 Dec 28 21:52 a.c
-rwxr-xr-x 1 CLC book 8479 Jan 9 22:27 a.out
-rw-r--r-- 1 CLC book 760 Jan 9 21:43 demo1.c
-rw-r--r-- 1 CLC book 711 Jan 9 21:43 demo2.c
-rw-r--r-- 1 CLC book 304 Jan 9 22:28 demo3.c
-rw-r--r-- 1 CLC book 0 Jan 2 00:16 demo3.config
-rw-r--r-- 1 CLC book 165 Jan 1 22:51 demo4.c
-rw-r--r-- 1 CLC book 440 Jan 9 22:25 demo5.c
-rw-r--r-- 1 CLC book 466 Jan 9 22:27 demo9.c
-rw-r--r-- 1 CLC book 0 Jan 1 23:50 tese.config
— 代表当前文件
r 代表可读权限
w 代表可写权限
x 可执行权限
可读是4 可写是2 可执行是1 0600 6表示4+2 后面的第一个0 表示同组 第二个0表示其他组
write()函数的用
以下是函数原型
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
读取成功返回的是字节数 读取失败返回的是-1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
main()
{
int fd;
char *buf = "zhaoyujian";
fd = open("./fili1",O_RDWR);
if(fd = -1)
{
printf("open filied\n");
fd = open("./fili1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
printf("sucessful\n");
}
}
printf("%d",fd);
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
运行结果是把buf中的数据写到fili1这个文件中-
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include<string.h>
int main()
{
char *buf = "zhaoyujian";
int fd = open("fili1",O_RDWR|O_APPEND);
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
~
O_RDWR|O_APPEND 如果文件已经有东西 则在之后继续写入数据 不删除原有的文件
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include<string.h>
int main()
{
char *buf = "zhaoyujian";
int fd = open("fili1",O_RDWR|O_TRUNC);
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
O_RDWR|O_TRUNC 如果文件中有数据则把所有的数据删除以后再把数据写进去
read()函数的用法
下面是函数原型
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
读取成功返回的是字节数 读取失败返回的是-1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
main()
{
int fd;
char *buf = "zhaoyujian";
fd = open("./fili1",O_RDWR);
if(fd = -1)
{
printf("open filied\n");
fd = open("./fili1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
printf("sucessful\n");
}
}
printf("%d",fd);
int n_write = write(fd,buf,strlen(buf));
printf("write %d\n ",n_write);
char *readbuf;
readbuf = (char *)malloc(sizeof(char)*n_write+1);
int n_read = read(fd,readbuf,n_write);
printf("read %d,%s",n_read,readbuf);
close(fd);
return 0;
}
这个代码是只有写入没有读取 没有将光标移动到最前面 有两中解决方案 一种只把打开的文件关闭重新打开 一种是用光标函数移动
close(fd);
open("fili1",O_RDWR);
关闭以后重新打开
lseek()函数的用法
下面是函数原型
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
返回值 偏移值的多少
whence
SEEK_SET 文件头
SEEK_END 文件尾
SEEK_CUR 当前位置
off_t offset 偏移值 对whence的偏移值
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
main()
{
int fd;
char *buf = "zhaoyujian";
fd = open("./fili1",O_RDWR);
if(fd = -1)
{
printf("open filied\n");
fd = open("./fili1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
printf("sucessful\n");
}
}
printf("%d",fd);
int n_write = write(fd,buf,strlen(buf));
printf("write %d\n ",n_write);
// close(fd);
// open("fili1",O_RDWR);
lseek(fd,1,SEEK_SET);
char *readbuf;
readbuf = (char *)malloc(sizeof(char)*n_write+1);
int n_read = read(fd,readbuf,n_write);
printf("read %d,%s",n_read,readbuf);
close(fd);
return 0;
}
上面说明了lseek用法 光标设置到最前面 偏移值如果是0 就只最前面 如果是1 就是从第二个开始 正数是光标向后移动 负数是光标向前移动
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
main()
{
int fd;
fd = open("./fili1",O_RDWR);
int n_lseek = lseek(fd,0,SEEK_END);
printf("%d",n_lseek);
close(fd);
return 0;
}
lseek用来计算文件的大小
文件描述符:
1,对于内核而言,所有打开文件都有文件描述符引用,文件描述符是一个非负整数,当打开一个现存文件或者创建一个新文件时,内核向进程返回一个文件描述符,当读写一个文件时,用open或write返回的文件描述符标识该文件,将其作为参考传递给read和write
按照惯例,UNIX shell使用文件描述符0与进程的标准输入输出相结合,文件描述符1与标准输出相结合,文件描述符2与标准错误输出相结合,STDIN_FILEO STDOUT_FILEO STDERR_FILENO 这几个宏代替了 0 1 2 这几个数字
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int fd;
char *buf;
buf = (char *)malloc(sizeof(char) * 5+1);
read(0,buf,5);
write(1,buf,5);
return 0;
}
标准输入输出文件的举例说明
2文件描述符,这个数字在一个进程中表示一个特定的含义,但我们open一个函数时,操作系统在内存中构建一些数据结构来表示这个动态文件,然后返回给应用程序一个数字作为文件描述符,这个数字就和我们内存中维护这个文件的这些数据结构绑定上了,以后我们应用程序如果要操作这个动态文件,只需要用这个文件描述符区分
3,文件描述符的作用域就是当前进程,出了这个进程文件描述符就没有意义了
1万+


 read() write() lseek() close() 函数的用法&spm=1001.2101.3001.11974&articleId=112407213&d=1&t=3&u=3324e675d7904e2fa14cecd1bce38ab2)

被折叠的 条评论
为什么被折叠?



