一、C库IO函数工作流程示意图:
FILE 类型的指针,是特殊结构体类型,包含文件描述符、读写指针位置、内存地址等信息,用于文件读写操作。
I/O缓冲区用于利用内存减少硬盘操作。在右侧三种情况下刷新缓冲区,存到硬盘上。
二、进程控制块PCB和文件描述符
文件描述符是int类型的。而且每个PCB的文件描述符表中的前三个都是固定的:
标准输入 fd为0
标准输出 fd为1
标准错误 fd为2
实际上,文件描述表是是个结构体指针数组:
三、虚拟地址空间
程序启动后,在磁盘上分配4G空间供进程使用,最多4G,用多少分多少。
0-3G在用户区,程序员可操作;3-4G为内核区,程序员不可操作。受保护的地址(0-4K)也不许用户访问,如NULL在此区域。程序从main函数开始执行,即从代码段执行,然后根据代码中变量类型等将元素分配到各个空间中。
四、库函数与系统函数的关系
由此可见,由于标准C库函数内部有一个缓冲区,所以可以等缓冲区满了以后再调用系统IO函数,所以效率提高了。
五、Linux系统IO函数
1、open函数
man 2 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);
参数:
flags设置:
O_RDONLY 以只读方式打开文件
O_WRONLY 以只写的方式打开文件
O_RDWR 以读写的方式打开
O_CREAT 如果文件不存在则创建文件
O_EXCL 如果文件存在,则强制 open() 操作失败
O_TRUNC 如果文件存在,将文件清零
O_APPEND 把文件添加内容的指针设到文件的结束处
mode 设置:
文件权限 = 给定对的文件权限 & 本地掩码(取反)
例如:
设定权限 0777
如果umask
出来的本地掩码是 0002
777 ----------------------------二进制 111 111 111
002 ----------------------------二进制 00 000 010 取反后得 111 111 101
111 111 111&111 111 101
实际权限 111 111 101
即实际权限为 0775
返回值:
若成功返回文件描述符(fd);若出错,返回-1
2、read函数
函数原型:
#include <uinstd.h>
ssize_t read(int fd, void *buf, size_t nbytes);
(读常规文件是不会阻塞的,不管读多少字节,read一定会在有限的时间内返回。从终端设备或网络读则不一定,如果从终端输入的数据没有换行符,调用read读终端设备就会阻塞,如果网络上没有接收到数据包,调用read从网络读就会阻塞,至于会阻塞多长时间也是不确定的,如果一直没有数据到达就一直阻塞在那里。同样,写常规文件是不会阻塞的,而向终端设备或网络写则不一定。)
返回值:
-1
:error会被置为相应的值。
error:为EAGAIN,表示在非阻塞下,此时无数据到达,立即返回。
error:为EINTR,表示被信号中断了。
0
:对端已关闭,本端也需要close 该套接字。
>0
:实际读取的数据长度。
3、write函数
函数原型:
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
返回值:
若成功,返回已写的字节数;若出错 返回 -1 ;
open,read,write 函数的运用:从一个文件汇总读取内容后,写入另一个文件中,自己动手写了个例子如下:
先写一个read_write.c
还有就是在实际中经常会有这样的需求:从某文件的前几个字节开始,读取文件内容,存到新文件中。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd = open("./test_read_006",O_RDONLY);
if(fd == -1)
{
exit(1);
}
int ret = lseek(fd,10,SEEK_SET);
int fd1 = open("./newfile",O_CREAT | O_WRONLY, 0777);
if(fd1 == -1)
{
exit(1);
}
char buf[1024];
memset(buf,0,sizeof(buf));
int count = read(fd,buf,sizeof(buf));
if(count == -1)
{
exit(1);
}
while(count)
{
int count1 = write(fd1,buf,count);
count = read(fd,buf,sizeof(buf));
}
close(fd);
close(fd1);
return 0;
}
再看一个常用例子,使用fwrite、fseek把文件中,从offset开始的len字节,全部写成0:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
FILE *fp = NULL;
int nRet = 0;
int zero_offset = 1;
int zero_size = 4094;
fp = fopen("./test4k_temp","r+");//r+
if(fp == NULL)
{
printf("open failed!\n");
exit(1);
}
int ret = fseek(fp,zero_offset,SEEK_SET);
char buf[4096];//buf长度要大于zero_size
memset(buf,0,sizeof(buf));
nRet = fwrite(buf,zero_size,1,fp);
if(nRet <= 0)
{
printf("fwrite failed!");
exit(1);
}
nRet = fclose(fp);
if(nRet != 0)
{
printf("close failed!\n");
exit(1);
}
return 0;
}
4、lseek函数
函数原型:
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
作用: 设置文件偏移量。
若文件的偏移量大于当前文件的长度,在这种情况下,对该文件的下一次写将加长该文件,并在文件中构成一个空洞。位于文件中没有写过的字节都被读为0.
文件中的空洞并不要求在磁盘上占用存储区。
参数:
whence的取值:
SEEK_SET 文件的偏移位置设置为距开始位置 offset 个字节
SEEK_CUR 文件的偏移位置设置为当前值 + offset ,offset 的值可正可负
SEEK_END 文件的偏移位置设置为文件长度 +offset ,offset 的值只能为正,只能向后拓展,不能向前拓展
返回值:
若成功返回从文件头部开始的偏移量,以字节为单位(即文件大小);若出错,返回-1
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
//打开一个已有的文件
int fd = open("./bb.txt",O_RDWR);
if( fd == -1)
{
perror("open bb.txt:");
exit(1);
}
int ret = lseek(fd,0,SEEK_END);
printf("file length = %d\n",ret);
// 文件扩展
ret = lseek(fd,2000,SEEK_END);
printf("return value = %d\n",ret);
// 实现文件扩展,需要最后一次写操作
write(fd,"a",1);
close(fd);
return 0;
}
5.获取文件属性—stat、lstat、fstat
函数原型
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf); //struct stat *buf 是函数外创建的,然后扔到函数内去赋值
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
参数
1.path :文件名或者目录名
2.fd : 文件描述符
3.
struct stat {
dev_t st_dev; //文件的设备编号
ino_t st_ino; //节点
mode_t st_mode; //文件的类型和存取的权限
nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
off_t st_size; //文件字节数(文件大小)
blksize_t st_blksize; //块大小(文件系统的I/O 缓冲区大小)
blkcnt_t st_blocks; //块数
time_t st_atime; //最后一次访问时间
time_t st_mtime; //最后一次修改时间
time_t st_ctime; //最后一次改变时间(指属性)
};
st_mode:下图中的是8进制下的数据
返回值:
若成功获取文件属性,返回0;若失败,返回 -1;
例子:
使用 stat() 函数实现一个简单的 ls -l 命令:
vi ls-l.c
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<pwd.h> // 所有者信息
#include<grp.h> // 所属组信息
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
if( argc<2 )
{
perror("参数不够");
exit(1);
}
struct stat st; //创建一个stat结构体的对象,供系统函数stat使用
int i;
for( i = 1; i<argc; i++)
{
int ret = stat(argv[i],&st); // 获取文件或者目录的所有信息存储于 st 结构体中
if( ret == -1 )
{
perror("stat");
exit(1);
}
// 存储文件类型和访问权限
char perms[11] = {0};
// 判断文件类型
switch( st.st_mode & S_IFMT )
{
case S_IFSOCK: // 套接字文件
perms[0] = 's';
break;
case S_IFLNK: // 软连接文件
perms[0] = 'l';
break;
case S_IFREG: // 普通文件
perms[0] = '-';
break;
case S_IFBLK: // 块设备文件
perms[0] = 'b';
break;
case S_IFDIR: // 目录文件
perms[0] = 'd';
break;
case S_IFCHR: // 字符设备文件
perms[0] = 'c';
break;
case S_IFIFO: // 管道文件
perms[0] = 'p';
break;
default:
break;
}
// 判断文件的访问权限
// 文件的所有者
perms[1] = (st.st_mode & S_IRUSR) ? 'r':'-';
perms[2] = (st.st_mode & S_IWUSR) ? 'w':'-';
perms[3] = (st.st_mode & S_IXUSR) ? 'x':'-';
// 文件的所属组
perms[4] = (st.st_mode & S_IRGRP) ? 'r':'-';
perms[5] = (st.st_mode & S_IWGRP) ? 'w':'-';
perms[6] = (st.st_mode & S_IXGRP) ? 'x':'-';
// 文件的其他用户
perms[7] = (st.st_mode & S_IROTH) ? 'r':'-';
perms[8] = (st.st_mode & S_IWOTH) ? 'w':'-';
perms[9] = (st.st_mode & S_IXOTH) ? 'x':'-';
// 硬链接计数
int nums = st.st_nlink;
// 文件所有者
char *fileuser = getpwuid(st.st_uid)->pw_name;
// 文件所属组
char *filegroup = getgrgid(st.st_gid)->gr_name;
// 文件大小
int size = (int)st.st_size;
// 文件修改时间
char *time = ctime(&st.st_mtime);
char mtime[512]="";
strncpy(mtime,time,strlen(time)-1);
// 保存输出信息格式
char buf[1024]={0};
// 把对应信息按格式输出到 buf 中
sprintf(buf,"%s %d %s %s %d %s %s",perms,nums,fileuser,filegroup,size,mtime,argv[i]);
// 打印 buf
printf("%s\n",buf);
}
return 0;
}
stat、lstat、fstat之间的区别:
1.fstat 函数:形参是”文件描述符”,而另外两个形参是“文件路径”。文件描述符是我们用 open 系统调用后得到的,而文件路径直接写就可以了。
2.stat 函数与 lstat 函数的区别: 当一个文件是软链接时,lstat 函数返回的是该软链接本身的信息 (不穿透);而 stat 函数返回的是该软链接指向文件的信息 (穿透)。
stat与 lstat 对比的例子:
vi ls-l.c
gcc ./ls-l.c -o ls-l
ln -s main.c main.soft
接下来我们要使用刚生成的可执行文件来观察main.c和其软链接main.soft的大小。
ls -l
./ls-l main.c main.soft
验证了确实stat具有穿透性,stat 函数返回的是该软链接指向文件的信息。
接下来我们把ls-l.c中的stat改成lstat,重新编译后重复执行上面步骤后再来观察:
验证了lstat不具有穿透性。
6.unlink
例:使用unlink的特性创建临时文件。
vi unlink.c
gcc unlink.c -o unlink
./unlink
7.其他常见的文件操作的系统IO
接下来看目录操作的系统IO
8.chdir
man 2 chdir
1.作用:修改当前进程的路径
2.函数原型:
#include <unistd.h>
int chdir(const char *path);
9.getcwd
1.作用:获取当前进程的工作目录
2.函数原型:
#include <unistd.h>
char *getcwd(char *buf, size_t size);
char *getwd(char *buf);
例子:chdir 函数和 getcwd 函数的运用:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main(int argc, char *argv[] )
{
if( argc<2 )
{
perror("参数不够");
exit(1);
}
printf(" agrv[1] = %s\n",argv[1]);
// 修改当前的路径
int ret =chdir(argv[1]);
if( ret == -1 )
{
perror("chdir");
exit(1);
}
// 在这里通过在改变后的目录下创建一个新的文件,来证明目录已经改变
int fd = open("chdir.txt",O_CREAT|O_RDWR,0644);
if( fd == -1 )
{
perror("open");
exit(1);
}
close(fd);
// 获取改变目录后的目录名
char buf[100]={0};
getcwd(buf,sizeof(buf));
printf("current dir: %s\n",buf);
return 0;
}
10.rmdir
1.作用:删除一个目录
2.函数原型:
#include <unistd.h>
int rmdir(const char *pathname);
11.mkdir
1.作用:创建一个目录
2.函数原型:
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
mode就是权限
12.opendir
1.作用:打开一个目录
2.函数原型
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
13.readdir
1.作用:读目录
2.函数原型:
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
3.返回值:返回一个记录项(即一个结构体对象)
14.closedir
1.作用:关闭一个目录
2.函数原型:
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
3.返回值:
若函数执行成功,返回0;若失败,返回 -1.
例子:递归读目录获取普通文件的个数
vi file_count.c
#include<unistd.h>
#include<dirent.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
// 获取 root 目录下的文件个数
int get_file_count(char *root)
{
// open dir
DIR * dir = NULL;
dir = opendir(root);
if( NULL == dir )
{
perror("opendir");
exit(1);
}
// 遍历当前打开的目录
struct dirent* ptr = NULL;
char path[1024]={0};
int total = 0;
while( (ptr = readdir(dir) )!= NULL)
{
// 过滤掉 . 和 ..
if( strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0 )
{
continue;
}
// 如果是目录,递归读目录
if(ptr->d_type == DT_DIR)
{
sprintf(path,"%s/%s",root,ptr->d_name);
total += get_file_count(path);
}
// 如果是普通文件
if( ptr->d_type == DT_REG )
{
total++;
}
}
// 关闭目录
closedir(dir);
return total;
}
int main(int argc,char *argv[])
{
if( argc<2 )
{
perror("参数不够");
exit(1);
}
// 获取指定目录下普通文件的个数
int count = get_file_count(argv[1]);
printf("%s has file numbers : %d\n",argv[1],count);
return 0;
}
15.dup和dup2(重定向文件描述符)
1.作用:复制现有的文件描述符
2.函数原型:
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
3.返回值:
(1).dup返回的是文件描述符中没有被占用的,然后就可以做到dup返回的文件描述符和oldfd都指向同一个文件。
(2).dup2 分两种情况讨论下:
(a).如果oldfd和newfd不相同,那么在拷贝前会先关掉newfd对应的文件,然后newfd被重定向,这样oldfd和newfd就都指向同一个文件了。
(b).如果oldfd和newfd是同一个文件描述符,不会关掉newfd , 直接返回oldfd,,这样显然oldfd和newfd也是指向同一个文件。
dup的例子如下:
这个例子证明了虽然一个文件可以有多个文件描述符,但是文件指针只有一个!!!!
vi dup.c
vi a.txt
先在a.txt里随便写一句话:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int fd =open("a.txt",O_RDWR);//打开一个已有的文件
if( fd == -1 )
{
perror("open");
exit(1);
}
printf("file open fd = %d\n",fd);
// 找到进程文件描述符表中第一个可用的文件描述符
// 将参数指定的文件复制到该描述后,返回这个描述符
int ret = dup(fd);
if( fd == -1 )
{
perror("dup");
exit(1);
}
printf(" dup fd = %d\n",ret);
char *buf = "你是猴子请来的救兵吗??\n";
char *buf1 = "你大爷的,我是程序猿!!!\n";
lseek(fd,0,SEEK_END);//注意,如果不想原有内容被覆盖,就要移动文件指针到末尾
write(fd,buf,strlen(buf));
write(ret,buf1,strlen(buf1));
close(fd);
return 0;
}
gcc ./dup.c -o ./dup
./dup
vi a.txt
dup2的例子如下:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int fd =open("english.txt",O_RDWR);
if( fd == -1 )
{
perror("open");
exit(1);
}
int fd1 =open("a.txt",O_RDWR);
if( fd1 == -1 )
{
perror("open");
exit(1);
}
printf("fd = %d\n",fd);
printf("fd1 = %d\n",fd1);
int ret = dup2(fd1, fd);//关闭fd也就是english.txt,然后fd被重定向,即fd也指向了a.txt
if( ret == -1 )
{
perror("dup2");
exit(1);
}
printf(" current fd = %d\n",ret);
char *buf = "主要看气质\n";
lseek(fd,0,SEEK_END);//注意,如果不想原有内容被覆盖,就要移动文件指针到末尾
write(fd,buf,strlen(buf));
write(fd1,"hello world!",12);
//以上两句都是对a.txt进行操作
close(fd);
close(fd1);
return 0;
}
16.fcntl(网络编程会用到)
fcntl的作用是改变已经打开的文件的属性。
fcntl的例子如下:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int flag;
int fd;
// 测试字符串
char *p = "我们是一个由中国特色社会主义的国家";
char *q ="社会主义好哇";
// 以只写方式打开文件
fd = open("test.txt",O_WRONLY);
if( fd == -1 )
{
perror("open");
exit(1);
}
// 输入新的内容,该内容会覆盖原来的内容
if( write(fd,p,strlen(p)) == -1 )
{
perror("write");
exit(1);
}
//使用 F_GETFL 命令得到文件状态标志
flag = fcntl(fd,F_GETFL,0); //第二个参数为F_GETFL时,第三个参数固定是0
if( flag == -1 )
{
perror("fcntl");
exit(1);
}
// 将文件状态标志添加 “追加写” 选项
flag |= O_APPEND;
// 将文件状态修改为追加写(注意,修改后文件指针会自动移到尾部!!!)
if( fcntl(fd,F_SETFL,flag) == -1 )
{
perror("fcntl");
exit(1);
}
// 再次输入新的内容,该内容会追加到最后
if( write(fd,q,strlen(q)) == -1 )
{
perror("write again");
exit(1);
}
return 0;
}