linux系统编程-文件操作(二):文件io函数【系统函数open/close/write/read/lseek】【不同于c语言的文件io库函数fopen/fclose/fputc/fgetc】_u013250861的博客-爱代码爱编程
一、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);
功能:
打开文件,如果文件不存在则可以选择创建。
参数:
pathname:文件的路径及文件名
flags:打开文件的行为标志,必选项 O_RDONLY, O_WRONLY, O_RDWR
mode:这个参数,只有在文件不存在时有效,指新建文件时指定文件的权限
返回值:
成功:成功返回打开的文件描述符
失败:-1
flags详细说明
必选项:
取值 | 含义 |
---|---|
O_RDONLY | 以只读的方式打开 |
O_WRONLY | 以只写的方式打开 |
O_RDWR | 以可读、可写的方式打开 |
可选项,和必选项按位或起来
取值 | 含义 |
---|---|
O_CREAT | 文件不存在则创建文件,使用此选项时需使用mode说明文件的权限 |
O_EXCL | 如果同时指定了O_CREAT,且文件已经存在,则出错 |
O_TRUNC | 如果文件存在,则清空文件内容 |
O_APPEND | 写文件时,数据添加到文件末尾 |
O_NONBLOCK | 对于设备文件, 以O_NONBLOCK方式打开可以做非阻塞I/O |
mode补充说明
1) 文件最终权限:mode & ~umask
2) shell进程的umask掩码可以用umask命令查看
- umask:查看掩码(补码)
- umask mode:设置掩码,mode为八进制数
- umask -S:查看各组用户的默认操作权限
取值 | 八进制 | 含义 |
---|---|---|
S_IRWXU | 00700 | 文件所有者的读、写、可执行权限 |
S_IRUSR | 00400 | 文件所有者的读权限 |
S_IWUSR | 00200 | 文件所有者的写权限 |
S_IXUSR | 00100 | 文件所有者的可执行权限 |
S_IRWXG | 00070 | 文件所有者同组用户的读、写、可执行权限 |
S_IRGRP | 00040 | 文件所有者同组用户的读权限 |
S_IWGRP | 00020 | 文件所有者同组用户的写权限 |
S_IXGRP | 00010 | 文件所有者同组用户的可执行权限 |
S_IRWXO | 00007 | 其他组用户的读、写、可执行权限 |
S_IROTH | 00004 | 其他组用户的读权限 |
S_IWOTH | 00002 | 其他组用户的写权限 |
S_IXOTH | 00001 | 其他组用户的可执行权限 |
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd = -1;
//1.以只读方式打开一个文件 如果文件不存在就报错
//fd = open("txt", O_RDONLY);
//2.以只写的方式打开一个文件 如果文件不存在就报错
//fd = open("txt", O_WRONLY);
//3.以只写的方式打开一个文件 如果文件不存在就创建, 如果文件存在就直接打开
//fd = open("txt", O_WRONLY | O_CREAT, 0644);
//4.以只读的方式打开一个文件 如果文件不存在就创建
//fd = open("txt", O_RDONLY | O_CREAT, 0644);
//5.以读写的方式打开文件 如果文件存在就报错, 如果文件不存在就创建
//fd = open("txt", O_RDWR | O_CREAT | O_EXCL, 0644);
//6.以读写的方式打开一个文件 如果文件不存在就创建 如果文件存在就清零
fd = open("txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (-1 == fd)
{
perror("open");
return 1;
}
printf("打开文件成功....\n");
return 0;
}
二、close函数
#include <unistd.h>
int close(int fd);
功能:
关闭已打开的文件
参数:
fd : 文件描述符,open()的返回值
返回值:
成功:0
失败: -1, 并设置errno
需要说明的是,当一个进程终止时,内核对该进程所有尚未关闭的文件描述符调用close关闭,所以即使用户程序不调用close,在终止时内核也会自动关闭它打开的所有文件。
但是对于一个长年累月运行的程序(比如网络服务器),打开的文件描述符一定要记得关闭,否则随着打开的文件越来越多,会占用大量文件描述符和系统资源。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#if 0
/* Standard file descriptors. */
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
#endif
int main(void)
{
int fd = -1;
//1. 打开文件
fd = open("txt", O_RDWR | O_CREAT, 0644);
if (-1 == fd)
{
perror("open");
return 1;
}
printf("fd = %d\n", fd);
printf("打开文件成功....\n");
//2. 关闭文件 释放资源
close(fd);
return 0;
}
三、read函数
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
功能:
把指定数目的数据读到内存(缓冲区)
参数:
fd : 文件描述符
buf : 内存首地址
count : 读取的字节个数
返回值:
成功:实际读取到的字节个数
失败: - 1
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 128
//从文件中读数据
int main(void)
{
int fd = -1;
int ret = -1;
char buf[SIZE];
//1. 打开文件 只读的方式
fd = open("txt", O_RDONLY);
if (-1 == fd)
{
perror("open");
return 1;
}
printf("open successfully fd = %d\n", fd);
//2. 读文件
memset(buf, 0, SIZE);
//从文件中最多读取SIZE个字节保存到buf中
ret = read(fd, buf, SIZE);
if (-1 == ret)
{
perror("read");
close(fd);
return 1;
}
printf("buf: %s\n", buf);
//3. 关闭文件
close(fd);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 128
int main(void)
{
int fd = -1;
int ret = -1;
char buf[SIZE];
//1. 打开文件
fd = open("passwd", O_RDONLY);
if (-1 == fd)
{
perror("open");
goto err0;
}
printf("打开文件ok... fd: %d\n", fd);
//2. 循环读取数据
while(1)
{
memset(buf, 0, SIZE);
//每一次从文件中读取最多SIZE个字节
ret = read(fd, buf, SIZE - 1);
if (ret < 0)
{
perror("read");
break;
}
printf("%s", buf);
//读到文件结尾
if (ret < SIZE - 1)
{
break;
}
}
//3. 关闭文件
close(fd);
return 0;
err0:
return 1;
}
四、write函数
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
功能:
把指定数目的数据写到文件(fd)
参数:
fd : 文件描述符
buf : 数据首地址
count : 写入数据的长度(字节)
返回值:
成功:实际写入数据的字节个数
失败: - 1
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
int fd = -1;
int ret = -1;
//1. 打开文件
//fd = open("txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
//fd = open("txt", O_WRONLY | O_CREAT , 0644);
fd = open("txt", O_WRONLY | O_CREAT | O_APPEND, 0644);
if (-1 == fd)
{
perror("open");
goto err0;
}
printf("fd = %d\n", fd);
//2. 写文件
ret = write(fd, "ABCDEFG", 7);
if (ret < 7)
{
perror("write");
}
//3. 关闭文件
close(fd);
return 0;
err0:
return 1;
}
五、lseek函数
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
功能:
改变文件的偏移量
参数:
fd:文件描述符
offset:根据whence来移动的位移数(偏移量),可以是正数,也可以负数,如果正数,则相对于whence往右移动,如果是负数,则相对于whence往左移动。如果向前移动的字节数超过了文件开头则出错返回,如果向后移动的字节数超过了文件末尾,再次写入时将增大文件尺寸。
whence:其取值如下:
SEEK_SET:从文件开头移动offset个字节
SEEK_CUR:从当前位置移动offset个字节
SEEK_END:从文件末尾移动offset个字节
返回值:
若lseek成功执行, 则返回新的偏移量
如果失败, 返回-1
所有打开的文件都有一个当前文件偏移量(current file offset),以下简称为 cfo。cfo 通常是一个非负整数,用于表明文件开始处到文件当前位置的字节数。
读写操作通常开始于 cfo,并且使 cfo 增大,增量为读写的字节数。文件被打开时,cfo 会被初始化为 0,除非使用了 O_APPEND 。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 128
//以十六进制的方式查看文件内容
//deng@itcast:~/share/4th$ od -x txt
int main(void)
{
int fd = -1;
int ret = -1;
//1. 打开文件
fd = open("txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (-1 == fd)
{
perror("open");
goto err1;
}
//在文件开头写七个字符
write(fd, "ABCDEFG", 7);
//将文件偏移30个字节
ret = lseek(fd, 1024 * 1024, SEEK_END);
if (-1 == ret)
{
perror("lseek");
goto err2;
}
write(fd, "123456789", 9);
//获取文件的大小
printf("file: %ld\n", lseek(fd, 0, SEEK_END));
//关闭文件
close(fd);
return 0;
err2:
close(fd);
err1:
return 1;
}
六、案例
1、文件的简单拷贝(不支持二进制)
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 128
//文件的简单拷贝 不支持二进制
int main(void)
{
int fdr = -1;
int fdw = -1;
int ret = -1;
char buf[SIZE];
//1. 以只读的方式打开第一个文件
fdr = open("passwd", O_RDONLY);
if (-1 == fdr)
{
perror("open");
goto err0;
}
//2. 以只写的方式打开第二个文件
fdw = open("txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (-1 == fdw)
{
perror("open");
goto err1;
}
//3. 循环拷贝数据
while(1)
{
//从第一个文件中读取数据
memset(buf, 0, SIZE);
ret = read(fdr, buf, SIZE);
if (ret <= 0)
{
perror("read");
break;
}
//将读取的数据写入到第二个文件中
ret = write(fdw, buf, ret);
if (ret <= 0)
{
break;
}
}
//4. 关闭两个文件描述符
close(fdr);
close(fdw);
return 0;
err1:
close(fdr);
err0:
return 1;
}
2、文件的简单拷贝(支持二进制)
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 128
//文件的简单拷贝 支持二进制
int main(int argc, char **argv)
{
int fdr = -1;
int fdw = -1;
int ret = -1;
char buf[SIZE];
//容错判断
if (3 != argc)
{
printf("usage: ./a.out filename1 filename2\n");
goto err0;
}
//1. 以只读的方式打开第一个文件
fdr = open(argv[1], O_RDONLY);
if (-1 == fdr)
{
perror("open");
goto err0;
}
//2. 以只写的方式打开第二个文件
fdw = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (-1 == fdw)
{
perror("open");
goto err1;
}
//3. 循环拷贝数据
while(1)
{
//从第一个文件中读取数据
memset(buf, 0, SIZE);
ret = read(fdr, buf, SIZE);
if (ret <= 0)
{
break;
}
//将读取的数据写入到第二个文件中
ret = write(fdw, buf, ret);
if (ret <= 0)
{
break;
}
}
//4. 关闭两个文件描述符
close(fdr);
close(fdw);
return 0;
err1:
close(fdr);
err0:
return 1;
}