目录
一、文件IO的概念
什么是文件I/O?
POSIX接口
核心概念是文件描述符(fd)
二、 文件IO的打开和关闭
1、文件IO-open
2、文件IO-close
三、文件的读写和定位
1、文件IO-read
2、文件IO-write
3、文件IO-lseek
day4作业:
一、文件IO的概念
什么是文件I/O?
文件IO,又称(系统IO,系统调用) 是操作系统提供的API接口函数。
POSIX接口
posix(可移植操作系统接口)定义的一组函数
不提供缓冲机制,每次读写操作都引起系统调用
核心概念是文件描述符(fd)
每个打开的文件都对应一个文件描述符。
文件描述符是一个非负整数。(标准IO是个结构体指针)Linux为程序中每个打开的文件分配一个文件描述符。
文件描述符从0开始分配,依次递增。
文件IO操作通过文件描述符来完成。
英文:缩写fd(file descriptor)
是0-1023的数字,表示文件。
0, 1, 2 的含义 标准输入,标准输出,错误
访问各种类型文件
Linux下, 标准IO基于文件IO实现
注意:文件IO不提供缓冲机制
二、 文件IO的打开和关闭
1、文件IO-open
open函数用来创建或打开一个文件:
#include
int open(const char *pathname, int flags); //不创建文件
int open(const char *pathname, int flags, mode_t mode); //创建文件,不能创建设备文件
成功时返回文件描述符;出错时返回EOF
打开文件时使用两个参数
创建文件时第三个参数指定新文件的权限,(只有在建立新文件时有效)此外真正建文件时的权限会受到umask 值影响,实际权限是mode-umaks
可以打开设备文件,但是不能创建设备文件(创建设备mknode 驱动部分会讲)
文件IO和标准的模式对应关系:(0664 普通用户权限)
r O_RDONLY
r+ O_RDWR
w O_WRONLY | O_CREAT | O_TRUNC, 0664
w+ O_RDWR | O_CREAT | O_TRUNC, 0664
a O_WRONLY | O_CREAT | O_APPEND, 0664
a+ O_RDWR | O_CREAT | O_APPEND, 0664
umask概念:
umask 用来设定文件或目录的初始权限
文件和目录的真正初始权限
文件或目录的初始权限 = 文件或目录的最大默认权限 - umask权限(0666-0002)
open_t.c:
#include#include #include #include int main(int argc, const char *argv[]) { int fd; fd = open("test.txt",O_WRONLY|O_CREAT|O_TRUNC,0666); if(fd<0){ printf("open file errn"); return 0; } printf("open successn"); }
结果
:
2、文件IO-close
close函数用来关闭一个打开的文件:
#include
int close(int fd);
成功时返回0;出错时返回EOF
程序结束时自动关闭所有打开的文件
文件关闭后,文件描述符不再代表文件
open_t.c:
#include#include #include #include #include #include #include int main(int argc, const char *argv[]) { int fd; int ret; fd = open("test.txt",O_WRONLY|O_CREAT|O_TRUNC,0666); if(fd==-1){ printf("open file errn"); return 0; } printf("success,fd= %dn",fd); ret = close(3);//运行过知道文件描述符是3,3就是fd if(ret<0){ printf("close file errn"); } ret = close(fd); printf("ret= %dn",ret); }
结果:
三、文件的读写和定位
1、文件IO-read
read函数用来从文件中读取数据:
#include
ssize_t read(int fd, void *buf, size_t count);//(文件描述符,缓冲区,读多少数据)
成功时返回实际读取的字节数;出错时返回EOF
读到文件末尾时返回0
buf是接收数据的缓冲区
count不应超过buf大小
2、文件IO-write
write函数用来向文件写入数据:
#include
ssize_t write(int fd, void *buf, size_t count);
成功时返回实际写入的字节数;出错时返回EOF
buf是发送数据的缓冲区
count不应超过buf大小
容易出错点:
求字符串长度应使用strlen,对二进制数据使用sizeof(strlen遇见 就结束)
printf 的字符应添加’ ’
3、文件IO-lseek
lseek函数用来定位文件:
#include
off_t lseek(int fd, off_t offset, intt whence); //(要定位的文件,偏移量,从哪儿偏移)
成功时返回当前的文件读写位置;出错时返回EOF
参数offset和参数whence同fseek完全一样
rdwr_t.c
#include#include #include #include #include #include int main(int argc,char *argv[]){ int fd; int ret; char buf[32] = "hello world"; char buf2[32]={0}; fd = open("test.txt",O_RDWR | O_CREAT|O_APPEND, 0666); if(fd<0){ printf("open file errn"); return 0; } printf("sucess,fd=%dn",fd); ret=write(fd,buf,strlen(buf));//sizeof(buf)就是32,而不是真正的字符串长度 if(ret<0){ perror("write"); goto END; } printf("write count=%dn",ret); lseek(fd,0,SEEK_SET);//文件IO定位 ret = read(fd,buf2,32); if(ret<0){ perror("read"); goto END; } buf2[31]=0; printf("read buf2=%sn",buf2); END: close(fd); }
结果:
day4作业:
使用文件IO实现“每隔1秒向文件1.txt写入当前系统时间,行号递增”
#include#include #include #include #include #include #include int main(int argc, const char *argv[]) { int fd; time_t ctime; struct tm *ctimestr; int ret; int linecount = 0; char buf2[100]; fd = open("wenjian1.txt",O_RDWR|O_CREAT|O_APPEND, 0666); if(fd==-1){ printf("open failedn"); return 0; } //计算 test.txt line while(read(fd,buf2,strlen(buf2)) != 0){ if (buf2[strlen(buf2)-1] == 'n'){//最后一个字符是回车,说明读完了 linecount++; } } while(1){ ctime = time(NULL); //printf("ctime=%dn",(int)ctime); ctimestr = localtime(&ctime); printf("%d, %04d-%02d-%02d %02d:%02d:%02dn",linecount,ctimestr->tm_year+1900,ctimestr->tm_mon+1,ctimestr->tm_mday, ctimestr->tm_hour,ctimestr->tm_min,ctimestr->tm_sec); sprintf(buf2,"%d, %04d-%02d-%02d %02d:%02d:%02dn",linecount,ctimestr->tm_year+1900,ctimestr->tm_mon+1,ctimestr->tm_mday, ctimestr->tm_hour,ctimestr->tm_min,ctimestr->tm_sec); //因为write的格式原因,所以只能先将printf的内容使用sprintf输出到字符串 ret = write(fd,buf2,strlen(buf2)); if(ret<0){ perror("write"); close(fd); } //fflush(fd);//如果不加这个函数,txt里面是空的,因为都放到缓冲区了 linecount++; sleep(1); } close(fd); }



