零、概述
文件IO 进程 进程间通信,线程,线程同步,网络编程
一、文件IO
IO(input output)
1.1 学习IO的前提
1.为什么要学习IO
嵌入式:由软件控制硬件,嵌入式需要对内存进行操作,和对内核进行操作,来完成操作硬件。
2.IO的内容都有什么:主要学习已经提供好的API函数接口
3.linux下一切皆文件
4.linux是一个多用户多任务的操作系统 ----》进程
5.因为IO进程在应用层和内核层之交互,所以说学习时定义过于抽象
6.linux操作系统把文件分为两大类
(1)二进制文件 —》a.out —>按照计算机的逻辑去存储
(2)文本文件 —》.c或者.txt —>按照程序员的逻辑去存储
7.底层存储是一样,逻辑存储上不一样。
1.2 IO是什么
IO指的是Input output,指的是对内存的输入和输出
1.3 如何使用IO
需要借助概念:linux下一切皆文件
所以说对文件的操作:打开文件---------读写文件----------关闭文件
1.4 IO的分类
1.文件IO:是由操作系统提供的API接口,也被称为系统调用,每个操作系统的IO—API接口是不一样的,也就造成了linux下的可执行我呢见如果调用了文件IO的话,是不能移植到别的平台的,因为文件IO的接口是由操作系统提供,不同操作系统接口是不一样的。
2.标准IO:stdio.h ----->是由标准C库 ANSI C库提供的API接口,标准IO是在文件IO的基础上封装出来的。
1.5 文件IO的接口
1. open 2. read 3. write 4. close 5. lseek 注意:windows函数接口:_open _read _write _close ios函数接口: Open Read Write Close
1.6 man手册的使用
man:是一个在线函数查询接口
1 Executable programs or shell commands( 可执行程序或者shell命令) 2 **System calls (functions provided by the kernel)(系统调用)(内核提供的函数) 3 **Library calls (functions within program libraries)(库调用)(程序库中的函数) 4 Special files (usually found in /dev) (特殊文件)(通常位于 /dev) 5 File formats and conventions eg /etc/passwd (文件格式和规范) 6 Games (游戏) 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 杂项(包括宏包和规范) 8 System administration commands (usually only for root) (系统管理命令)(通常只用于管理用户) 9 Kernel routines [Non standard](内核例程)【非标准】
1.7 errno
errno是一个全局变量,存在于#include 中,用于标注错误信息,那么当函数报错返回时,会将这个全局变量重新根据错误信息表中的对用的号码设置一下
1.8 文件描述符
文件描述符是什么:一个文件的标志,是一个int类型的数据,非负数,值是小的。
注意:
./aout在刚运行时,内核会自动为他打开三个设备文件,返回三个文件描述:
1.标准输入 -----0
2.标准输出 -----1
3.标准错误输出 ----2
1.9 API接口的使用
附加:函数的三要素:功能,参数,返回值
1.9 1 open
1.头文件: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> 2.原型: int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); 3.功能:打开一个文件 4.参数: pathname:路径及名称 例如:"1.txt" //以后参数是char*类型一般直接写字符串 flags:打开文件的方式 必须包括: O_RDONLY:只读权限打开文件 O_WRONLY:只写权限打开文件 O_RDWR:读写方式打开文件 附加参数(以或的形式去附加) O_APPEND:以追加写的方式打开文件 O_CREAT:如果文件存在则打开文件,如果文件不存在则创建文件,如果使用了O_CREAT这个参数,需要使用open的第三个参数,给与权限是一个8进制的整形,以0开头 例如:0664 O_EXCL:如果文件存在则报错返回 O_NONBLOCK:以非阻塞方式打开一个文件 O_TRUNC:以清空的方式打开文件 5.返回值: 成功返回一个文件描述符 失败返回-1; 注意:使用0666创建文件为什么出来的是0664呢? 是由操作系统有一个叫做文件掩码的机制,将其它用户权限的可写权限抹掉 使用umask查看文件掩码 修改文件掩码 umask + 想要修改的权限值
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> int main(int argc, char const *argv[]) { int fd = open("./1.txt",O_RDWR|O_CREAT,0666); //打开一个文件 if(-1 == fd) { perror("open"); //打印错误信息 printf("errno = %d\n",errno); return -1; } perror("open1"); return 0; }
1.9.2 perror
头文件: #include 原型: void perror(const char *s); 功能:以固定格式打印错误信息 参数:s:判断的函数名字 返回值:无
1.9.3 read
头文件: #include 原型: ssize_t read(int fd, void *buf, size_t count); 功能:对一个文件进行读取 参数:fd:目标文件描述符 ----》对哪个文件进行读取 buf:存放读到的数据 ----》读到的数据存放的地址 count:读多少 返回值:成功:返回读到的字节个数 失败:返回-1 读到文件末尾返回0 注意:但是最后依次read时如果文件剩余字节不够read的count,读到文件末尾时返回已经读到的字节个数,再一次读取时返回0
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> #include <unistd.h> int main(int argc, char const *argv[]) { //打开文件----》以读的方式打开 int fd = open("./1.txt",O_RDONLY); if(-1 == fd) { perror("open"); return -1; } //开始读取数据 char buf[3] = {0}; if(-1 == read(fd,buf,3)) //有多少空间读取多少个字节 { perror("read"); return -1; } printf("\n读取到的值为buf = %s\n",buf); //关闭文件描述符 if(-1 == close(fd)) { perror("close"); return -1; } return 0; }
1.9.4 write
头文件: #include 原型: ssize_t write(int fd, const void *buf, size_t count); 功能:对一个文件进行写入操作 参数:fd:目标文件描述符 ----》对哪个文件进行操作 buf:要写入的数据的地址----》要写入什么东西 count:写入数据的大小 ---》写多少,以字节为单 返回值: 成功:返回写入的字节个数 失败: -1
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include <string.h> int main(int argc, char const *argv[]) { int fd = open("./1.txt",O_WRONLY); if(-1 == fd) { perror("open"); return -1; } //开始写入数据 char buf[123] = "hello world"; if(-1 == write(fd,buf,strlen(buf))) { perror("write"); return -1; } return 0; }
1.9.5 close
头文件: #include 原型: int close(int fd); 功能:关闭一个文件 参数:fd要关闭的文件描述符 返回值: 成功:返回 0 失败: -1 注意:一个可执行程序最多打开1024个文件描述符,这个值可以通过内核去修改。 练习:先对一个文件进行写入helloworld,然后在从这个文件将helloworld读出来,打印到终端上。 结果:写入数据时正常,读取时未读到数据,这是因为读写指针的相互影响
1.9.6 lseek
头文件: #include <sys/types.h> #include <unistd.h> 原型: off_t lseek(int fd, off_t offset, int whence); 功能:操作读写指针,对读写指针进行偏移 参数: fd:目标文件描述符 ---》对哪个文件进行操作 offset:如何偏移,偏移多少 如果该数为负数,代表这向前进行偏移,如果偏移出了文件的开头,会报错返回 如果该数为正数,代表这向后偏移,如果偏移除了文件的末尾,会扩大文件,用'\0'来填充,那么此类文件被称为---空洞文件--- 注意:如果偏移后没有对其进行任何写入操作内核认为该偏移无效,不会扩大文件大小。 whence:基准位置 ----》根据哪一个位置进行偏移 SEEK_SET :根据文件开头进行偏移 The file offset is set to offset bytes. SEEK_CUR:根据用户当前位置进行偏移 The file offset is set to its current location plus offset bytes. SEEK_END:根据文件末尾进行偏移 The file offset is set to the size of the file plus offset bytes. 返回值: 成功:返回偏移的字节个数(根据文件开头来定) 失败: -1
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include <string.h> int main(int argc, char const *argv[]) { int fd = open(argv[1],O_RDWR|O_CREAT,0666); if(-1 == fd) { perror("open"); return -1; } //开始进行写入数据 if(-1 == write(fd,"hello world",11)) { perror("write"); return -1; } if(-1 == lseek(fd,-11,SEEK_END)) { perror("lseek"); return -1; } char buf[123] = {0}; if(-1 == read(fd,buf,sizeof(buf))) { perror("read"); return -1; } printf("buf = %s\n",buf); close(fd); return 0; }
1.10 练习:自己去实现cp命令 mycp(保证可以去拷贝二进制文件)
//读取时循环读取,写入时循环写入,使用argv参数 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include <string.h> #define SIZE 123 int main(int argc, char const *argv[]) { if(3 != argc) { printf("输入参数错误,例如:./a.out + 源文件 + 目标文件\n"); return -1; } //打开要复制的文件以只读方式打开,打开要粘贴的文件,以写入并创建并且清空 int fd = open(argv[1],O_RDONLY); if(-1 == fd) { perror("open"); return -1; } int fd1 = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664); if(-1 == fd1) { perror("open1"); return -1; } //开始读取 ---》边读边写 while(1) { //先读 char buf[SIZE] = {0};//作为数据的中转站 ssize_t ret = read(fd,buf,sizeof(buf)); if(-1 == ret) { perror("read"); return -1; } if(0 == ret) { printf("读取到文件的末尾,退出while循环\n"); break; } //写入 if(-1 == write(fd1,buf,ret)) { perror("write"); return -1; } } close(fd); close(fd1); return 0; }




