商务网站建设联系方式百度seo关键词排名 s
目录
文件理解
狭义
广义
文件
C接口
写文件
读文件
简易cat
示例
stdin&&stdout&&stderr
系统调用
标志位
写文件(系统调用)
读文件
文件描述符fd
文件描述符的分配规则
重定向
dup2
⼀切皆⽂件
缓冲区
为什么
缓冲类型
文件理解
狭义
文件存储在磁盘中,磁盘是外设,磁盘上的文件本质是对⽂件的所有操作,都是对外设的输⼊和输出简称IO
广义
linux下一切皆文件
文件
文件包括内容加属性
0KB的文件占用磁盘空间(因为文件包括文件属性)
所有文件操作本质为对文件属性与内容的操作
C接口
#include <stdio.h>
int main()
{FILE *fp = fopen("myfile", "w");//打开,没有就创建,以只写方式打开if(!fp){//文件没有打开printf("fopen error!\n");}while(1);fclose(fp);return 0;
}
写文件
#include <stdio.h>
#include <string.h>
int main()
{FILE *fp = fopen("myfile", "w");if(!fp){printf("fopen error!\n");}const char *msg = "hello bit!\n";int count = 5;//执行次数while(count--){fwrite(msg, strlen(msg), 1, fp);//写入文件fd,写入内容为msg,大小为msg的大小,写入次数为1}fclose(fp);//关闭文件return 0;
}
读文件
#include <stdio.h>
#include <string.h>
int main()
{FILE *fp = fopen("myfile", "r");//只读方式打开文件,没有不会创建if(!fp){//没有该文件printf("fopen error!\n");return 1;}char buf[1024];//读取缓冲区const char *msg = "hello bit!\n";
while(1){ssize_t s = fread(buf, 1, strlen(msg), fp);//返回读取到的字符个数,读取次数为1if(s > 0){buf[s] = 0;printf("%s", buf);}if(feof(fp)){//达到文件结尾break;}}fclose(fp);return 0;
}
简易cat
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{if (argc != 2) {printf("argv error!\n");return 1;}FILE *fp = fopen(argv[1], "r");if(!fp){printf("fopen error!\n");return 2;}char buf[1024];while(1){int s = fread(buf, 1, sizeof(buf), fp);if(s > 0){buf[s] = 0;printf("%s", buf);}if(feof(fp)){break;}}
fclose(fp);return 0;
}
示例
输出信息到显示器的方法
1.printf
2.fprintf
3.fwrite
#include <stdio.h>
#include <string.h>
int main()
{const char *msg = "hello fwrite\n";fwrite(msg, strlen(msg), 1, stdout);printf("hello printf\n");fprintf(stdout, "hello fprintf\n");return 0;
}
stdin&&stdout&&stderr
C默认会打开三个输⼊输出流,分别是stdin,stdout,stderr
类型均为FILE*
系统调用
标志位
#include <stdio.h>
#define ONE 0001 //0000 0001
#define TWO 0002 //0000 0010
#define THREE 0004 //0000 0100
void func(int flags) {if (flags & ONE) printf("flags has ONE! ");if (flags & TWO) printf("flags has TWO! ");if (flags & THREE) printf("flags has THREE! ");printf("\n");
}
int main() {func(ONE);func(THREE);func(ONE | TWO);func(ONE | THREE | TWO);return 0;
}
写文件(系统调用)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{umask(0);//设置权限掩码为0int fd = open("myfile", O_WRONLY|O_CREAT, 0644);//读方式打开,没有文件就创建,权限0644if(fd < 0){perror("open");return 1;}int count = 5;const char *msg = "hello bit!\n";int len = strlen(msg);while(count--){write(fd, msg, len);//fd: 后⾯讲, msg:缓冲区⾸地址, len: 本次读取,期望写
⼊多少个字节的数据。 返回值:实际写了多少字节数据 }close(fd);return 0;
}
读文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{int fd = open("myfile", O_RDONLY);if(fd < 0){perror("open");return 1;}const char *msg = "hello bit!\n";char buf[1024];while(1){ssize_t s = read(fd, buf, strlen(msg));//类⽐write if(s > 0){printf("%s", buf);}else{break;}}close(fd);return 0;
}
文件描述符fd
Linux进程默认情况下会有3个缺省打开的⽂件描述符,分别是标准输⼊0,标准输出1,标准错 误2.
0,1,2对应的物理设备⼀般是:键盘,显⽰器,显⽰器
文件描述符为一个整数
文件描述符的分配规则
⽂件描述符的分配规则:在files_struct数组当中,找到 当前没有被使⽤的最⼩的⼀个下标,作为新的⽂件描述符
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{close(0);//close(2);int fd = open("myfile", O_RDONLY);if(fd < 0){perror("open");return 1;}printf("fd: %d\n", fd);close(fd);return 0;
}
输出 fd:0
重定向
如果是关掉文件描述符1,然后创建一个新文件,那么它的fd为1,此时本来应该会向标准输出(即显示器文件)打印的消息会放入这个创建的新文件中
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{close(1);int fd = open("myfile", O_WRONLY|O_CREAT, 00644);if(fd < 0){perror("open");return 1;}printf("fd: %d\n", fd);fflush(stdout);
close(fd);exit(0);
}
像关闭文件描述符1叫做输出重定向
常见的重定向有>,>>,<
dup2
原型
#include <unistd.h>
int dup2(int oldfd, int newfd);
示例
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {int fd = open("./log", O_CREAT | O_RDWR);if (fd < 0) {perror("open");return 1;}
close(1);dup2(fd, 1);for (;;) {char buf[1024] = {0};ssize_t read_size = read(0, buf, sizeof(buf) - 1);if (read_size < 0) {perror("read");break;}printf("%s", buf);fflush(stdout);}return 0;
}
printf是C库当中的IO函数,⼀般往stdout中输出,但是stdout底层访问⽂件的时候,找的还是fd:1,但此时,fd:1下标所表⽰内容,已经变成了myfifile的地址,不再是显⽰器⽂件的地址,所以,输出的 任何消息都会往⽂件中写⼊,进⽽完成输出重定向
⼀切皆⽂件
⾸先,在windows中是⽂件的东西,它们在linux中也是⽂件;其次⼀些在windows中不是⽂件的东 西,⽐如进程、磁盘、显⽰器、键盘这样硬件设备也被抽象成了⽂件,你可以使⽤访问⽂件的⽅法访 问它们获得信息;
当打开⼀个⽂件时,操作系统为了管理所打开的⽂件,都会为这个⽂件创建⼀个file结构体
值得关注的是 struct file 中的 f_op 指针指向了⼀个 file_operations 结构体,这个结构 体中的成员除了struct module*owner其余都是函数指针。
file_operation 就是把系统调⽤和驱动程序关联起来的关键数据结构,这个结构的每⼀个成员都 对应着⼀个系统调⽤。读取 file_operation 中相应的函数指针,接着把控制权转交给函数,从⽽ 完成了Linux设备驱动程序的⼯作
缓冲区
缓冲区是内存空间的⼀部分。也就是说,在内存空间中预留了⼀定的存储空间,这些存储空间⽤来缓 冲输⼊或输出的数据,这部分预留的空间就叫做缓冲区。缓冲区根据其对应的是输⼊设备还是输出设 备,分为输⼊缓冲区和输出缓冲区。
为什么
读写⽂件时,如果不会开辟对⽂件操作的缓冲区,直接通过系统调⽤对磁盘进⾏操作(读、写等),那么 每次对⽂件进⾏⼀次读写操作时,都需要使⽤读写系统调⽤来处理此操作,即需要执⾏⼀次系统调 ⽤,执⾏⼀次系统调⽤将涉及到CPU状态的切换,即从⽤⼾空间切换到内核空间,实现进程上下⽂的 切换,这将损耗⼀定的CPU时间,频繁的磁盘访问对程序的执⾏效率造成很⼤的影响
为了减少使⽤系统调⽤的次数,提⾼效率,我们就可以采⽤缓冲机制。⽐如我们从磁盘⾥取信息,可 以在磁盘⽂件进⾏操作时,可以⼀次从⽂件中读出⼤量的数据到缓冲区中,以后对这部分的访问就不 需要再使⽤系统调⽤了,等缓冲区的数据取完后再去磁盘中读取,这样就可以减少磁盘的读写次数, 再加上计算机对缓冲区的操作⼤ 快于对磁盘的操作,故应⽤缓冲区可⼤ 提⾼计算机的运⾏速度
缓冲类型
标准I/O提供了3种类型的缓冲区
全缓冲区:这种缓冲⽅式要求填满整个缓冲区后才进⾏I/O系统调⽤操作。对于磁盘⽂件的操作通 常使⽤全缓冲的⽅式访问。
⾏缓冲区:在⾏缓冲情况下,当在输⼊和输出中遇到换⾏符时,标准I/O库函数将会执⾏系统调⽤ 操作。当所操作的流涉及⼀个终端时(例如标准输⼊和标准输出),使⽤⾏缓冲⽅式。因为标准 I/O库每⾏的缓冲区⻓度是固定的,所以只要填满了缓冲区,即使还没有遇到换⾏符,也会执⾏ I/O系统调⽤操作,默认⾏缓冲区的⼤⼩为1024。
⽆缓冲区:⽆缓冲区是指标准I/O库不对字符进⾏缓存,直接调⽤系统调⽤。标准出错流stderr通 常是不带缓冲区的,这使得出错信息能够尽快地显⽰出来。
除了上述列举的默认刷新⽅式,下列特殊情况也会引发缓冲区的刷新:
1. 缓冲区满时;
2. 执⾏flush语句;