文章目录
- 1.什么是文件IO
- 1.1 概念
- 1.2 特点
- 1.3 操作
- 2.函数接口
- 2.1 打开文件 open()
- 2.2 关闭文件
- 2.3 读写文件
- 2.3.1 读文件
- 2.3.2 写文件
- 2.4 文件的定位操作
- 标准IO和文件IO总结
1.什么是文件IO
1.1 概念
又称系统IO,是系统调用,是操作系统提供的接口函数。
posix中定义的一组用于输入输出的函数
POSIX接口 (英语:Portable Operating System Interface)可移植操作系统接口
1.2 特点
(1)没有缓存机制,每次调用都会引起系统调用。
(2)围绕文件描述符进行操作,非负整数(>=0),依次分配
(3)文件IO默认打开三个文件描述符,分别是0(标准输入),1(标准输出),2(标准错误)
(4)操作任意类型的文件b c - l s p (除了d目录文件)
1.3 操作
打开文件: open
关闭文件:close
读写操作:read、write
定位操作: lseek
2.函数接口
2.1 打开文件 open()
int open(const char *pathname, int flags);
功能:打开文件
参数:pathname:文件路径名flags:打开文件的方式O_RDONLY:只读O_WRONLY:只写O_RDWR:可读可写O_CREAT:不存在创建O_TRUNC:存在清空O_APPEND:追加
返回值:成功:文件描述符失败:-1当第二个参数中有O_CREAT选项时,需要给open函数传递第三个参数,指定创建文件的权限 int open(const char *pathname, int flags, mode_t mode);
最后权限=创建出来的文件指定权限值&(~umask)
例如:指定权限为0666(8进制)
最终权限为:0666 & (~umask) = 0666&0775 = 0664
例子
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main(int argc, char const *argv[])
{int fd;//fd = open("a.c",O_WRONLY|O_CREAT|O_TRUNC,0666);fd = open("b.c",O_RDONLY);if(fd < 0){perror("open err");return -1;}printf("fd:%d\n",fd);return 0;
}
标准IO | 文件IO |
---|---|
r | O_RDONLY 只读 |
r+ | O_RDWR 可读可写 |
w | O_WRONLY|O_CREAT|O_TRUNC,0666 只写,不存在创建,存在清空 |
w+ | O_RDWR|O_CREAT|O_TRUNC,0666 可读可写,不存在创建,存在清空 |
a | O_WRONLY|O_CREAT|O_APPEND,0666 只写,不存在创建,存在追加 |
a+ | O_RDWR|O_CREAT|O_APPEND,0666可读可写,不能存在创建,存在追加 |
注意:O_CREAT 需要加权限
2.2 关闭文件
int close(int fd);
功能:关闭文件
参数:fd:文件描述符
2.3 读写文件
2.3.1 读文件
ssize_t read(int fd, void *buf, size_t count);
功能:从一个已打开的可读文件中读取数据
参数: fd 文件描述符buf 存放位置count 期望的个数
返回值:成功:实际读到的个数(小于期望值说明实际没这么多)返回0:表示读到文件结尾返回-1:表示出错,并设置errno号
fgetc -> EOF 失败或末尾
fgets -> NULL 失败或末尾
fread -> 0 失败或末尾
read -> -1失败 0末尾
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, char const *argv[])
{int fd;char buf[32]="";fd = open("a.c", O_RDONLY);if (fd < 0){perror("open err");return -1;}printf("fd:%d\n", fd);read(fd,buf,10); //向fd文件中读取10个字符printf("buf: %s\n",buf);close(fd);return 0;
}
2.3.2 写文件
ssize_t write(int fd, const void *buf, size_t count);
功能:向指定文件描述符中,写入 count个字节的数据。
参数:fd 文件描述符buf 要写的内容count 期望写入字节数
返回值:成功:实际写入数据的个数失败 : -1
例子
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, char const *argv[])
{int fd;char buf[32]="hello";fd = open("a.c", O_RDWR|O_CREAT|O_TRUNC,0666);if (fd < 0){perror("open err");return -1;}printf("fd:%d\n", fd);write(fd,buf,5);write(fd,"haha",10);close(fd);return 0;
}
2.4 文件的定位操作
off_t lseek(int fd, off_t offset, int whence);
功能:设定文件的偏移位置
参数:fd:文件描述符offset: 偏移量 正数:向文件结尾位置移动负数:向文件开始位置whence: 相对位置SEEK_SET 开始位置SEEK_CUR 当前位置SEEK_END 结尾位置
补充:和fseek一样其中SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2.返回值:成功:文件的当前位置失败:-1
例子
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, char const *argv[])
{int fd;char buf[32] = "";fd = open("a.c", O_RDWR | O_CREAT | O_TRUNC, 0666);if (fd < 0){perror("open err");return -1;}printf("fd:%d\n", fd);// write(fd, "66666", 10);// lseek(fd,0,SEEK_SET); //定位到文件开头// read(fd, buf, 10);// printf("%s\n", buf);lseek(fd,10,2); //定位到末尾后10个字符单位write(fd,"k",1);off_t off = lseek(fd,0,SEEK_END); //利用结尾偏移求出当前长度printf("%ld\n",off);close(fd);return 0;
}
标准IO和文件IO总结
标准IO | 文件IO | |
---|---|---|
概念 | C库中定义的一组用于输入输出的函数 | posix中定义的一组用于输入输出的函数 |
特点 | 1.有缓冲机制 2.围绕流进行操作,FILE *3.默认打开三个流:stdin/stdout/stderr4.只能操作普通文件5.程序的可移植性更强 | 1.无缓冲机制2.围绕着文件描述符,非负整数3.默认打开三个文件描述符:0/1/24.可以操作除了目录以外的任意类型文件 |
函数 | 打开文件:fopen、freopen 关闭文件:fclose 读文件:fgetc/fgets/fread 写文件:fputc/fputs/fwrite 定位操作:rewind/fseek/ftell | 打开文件: open 关闭文件: close 读文件: read 写文件:write 定位操作: lseek |