一、C语言中文件IO操作

1.文件的打开方式总结

r Open text file for reading.
The stream is positioned at the beginning of the file.
只读方式打开,文件指针指向文件的首位置

r+ Open for reading and writing.
The stream is positioned at the beginning of the file.
读写方式打开,文件指针指向文件的首位置

w Truncate(缩短) file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
只写方式打开,清空文件内容,如果文件不存在则创建文件,文件指针指向文件的首位置

w+ Open for reading and writing.
The file is created if it does not exist, otherwise it is truncated.
The stream is positioned at the beginning of the file.
读写方式打开,文件不存在则创建文件,如果文件存在则清空原来内容,文件指针指向文章的首位置

a **Open for appending (writing at end of file).
The file is created if it does not exist.
The stream is positioned at the end of the file.**
以追加写的方式打开,写到文件的结尾处,文件不存在则创建文件,文件指针指向文件的末尾位置

a+ **Open for reading and appending (writing at end of file).
The file is created if it does not exist.
The initial file position for reading is at the beginning of the file,
but output is always appended to the end of the file.**
以读和追加写的方式打开,文件不存在则创建文件,如果文件存在则从文件的末尾位置开始追加

2.fopen打开文件

3.fread写文件

1    #include<stdio.h>
     2    #include<string.h>
     3    #include<stdlib.h>
     4    int main()
     5    {
     6        FILE* fp=fopen("test.txt","w+");
     7        if(fp==NULL)
     8        {
     9            printf("文件打开失败!\n");
    10            exit(1);
    11        }
    12        fwrite("bit person!",sizeof(char),11,fp);
    13        fseek(fp,0,SEEK_CUR);
    14        char arr[100];
    15        fread(arr,sizeof(char),strlen(arr),fp);
            printf("%s\n",arr);
    16        fclose(fp);
    17        return 0;
    18    }
12345678910111213141516171819

4.fwrite写文件

1    #include<stdio.h>
     2    #include<string.h>
     3    #include<stdlib.h>
     4    int main()
     5    {
     6        FILE* fp=fopen("test.txt","a+");
     7        if(fp==NULL)
     8        {
     9            printf("打开文件失败!\n");
    10            exit(1);
    11        }
    12        char *arr="bit education!\n";
    13        fwrite(arr,sizeof(char),strlen(arr),fp);
    14        fclose(fp);
    15        return 0;
    16    }
12345678910111213141516

5.fseek移动文件指针


6.ftell获取文件指针当前位置

7.rewind让文件指针回到文件起始位置

8.fcloes关闭文件

9.输出信息到显示器方法

fwrite(msg,strlen(msg),sizeof(char),fp);//往log.txt文件里面写

1    #include<stdio.h>
     2    #include<string.h>
     3    int main()
     4    {
     5        FILE *fp=fopen("log.txt","w");
     6        if(NULL==fp)
     7        {
     8            printf("打开文件失败!\n");
     9        }
    10        else 
    11        {
    12           /* char c='A';
    13            for(;c<'Z';c++)
    14            {
    15                fputc(c,fp);
    16            }*/
    17            const char* msg="Hello bit!\n";
    18           // fwrite(msg,strlen(msg),sizeof(char),fp);//往log.txt文件里面写
    19            fwrite(msg,strlen(msg),sizeof(char),stdout);//输出,往显示屏上输出
    20    
    21        }
    22        fclose(fp);
    23        return 0;
    24    }
123456789101112131415161718192021222324

fwrite(msg,strlen(msg),sizeof(char),stdout);//输出,往显示屏上输出

10.stdin & stdout & stderr

  • C默认会打开三个输入输出流,分别是stdin, stdout, stderr
  • 仔细观察发现,这三个流的类型都是FILE, fopen返回值类型,文件指针

二、系统文件I/O

1.read和write的初次使用

操作文件,除了上述C接口(当然,C++也有接口,其他语言也有),我们还可以采用系统接口来进行文件访问,先来直接以代码的形式,实现和上面一模一样的代码:

写文件

1    #include<stdio.h>
     2    #include<stdlib.h>
     3    #include<sys/stat.h>
     4    #include<fcntl.h>
     5    #include<unistd.h>
     6    #include<string.h>
     
     7    int main()
     8    {
     9       int fd=open("myfile",O_WRONLY|O_CREAT,0644);
    10       if(fd<0)
    11       {
    12           printf("文件打开失败!\n");
    13           exit(1);
    14       }
    15       char* arr="bit education!";
    16       write(fd,arr,strlen(arr)-1);
    17       close(fd);
    18       return 0;
    19    }
1234567891011121314151617181920


读文件

1    #include<stdio.h>
     2    #include<stdlib.h>
     3    #include<string.h>
     4    #include<fcntl.h>
     5    #include<sys/stat.h>
     6    #include<unistd.h>
     7    int main()
     8    {
     9        int fd=open("myfile.txt",O_RDONLY);
    10        if(fd<0)
    11        {
    12            printf("文件打开失败!\n");
    13            exit(1);
    14        }
    15        char* arr="hello bit!";
    16        char buff[100];
    17        read(fd,buff,strlen(arr)-1);
    18        printf("%s\n",buff);
    19        close(fd);
    20        return 0;
    21    }
123456789101112131415161718192021

2.接口介绍

open man 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);
pathname: 要打开或创建的目标文件
flags: 打开文件时,可以传入多个参数选项,用下面的一个或者多个常量进行“或”运算,构成flags。
参数:
O_RDONLY: 只读打开
O_WRONLY: 只写打开
O_RDWR : 读,写打开
这三个常量,必须指定一个且只能指定一个
O_CREAT : 若文件不存在,则创建它。需要使用mode选项,来指明新文件的访问权限
O_APPEND: 追加写
返回值:
成功:新打开的文件描述符
失败:-1

mode_t理解:直接 man 手册,比什么都清楚。

open 函数具体使用哪个,和具体应用场景相关,如目标文件不存在,需要open创建,则第三个参数表示创建文件的默认权限,否则,使用两个参数的open。

3.open函数返回值

  • [ ] 在认识返回值之前,先来认识一下两个概念: 系统调用 和 库函数
  • 上面的 fopen fclose fread fwrite 都是C标准库当中的函数,我们称之为库函数(libc)。
  • 而open close read write lseek 都属于系统提供的接口,称之为系统调用接口。
  • 回忆一下我们讲操作系统概念时,画的一张图。


系统调用接口和库函数的关系,一目了然。所以,可以认为,f#系列的函数,都是对系统调用的封装,方便二次开发。

4.文件描述符fd(file descriptor)

  • 通过对open函数的学习,我们知道了文件描述符就是一个小整数。

5.0 & 1 & 2

  • Linux进程默认情况下会有3个缺省打开的文件描述符,分别是标准输入0, 标准输出1, 标准错误2。
  • 0,1,2对应的物理设备一般是:键盘,显示器,显示器。
1    #include<stdio.h>
     2    #include<string.h>
     3    #include<stdlib.h>
     4    #include<fcntl.h>
     5    #include<unistd.h>
     6    int main()
     7    {
     8        char buff[100];
     9        int fd=read(0,buff,sizeof(buff));
    10        write(1,buff,strlen(buff)-1);
    11        write(2,buff,strlen(buff)-1);
    12        return 0;
    13    }
12345678910111213


而现在知道,文件描述符就是从0开始的小整数。当我们打开文件时,操作系统在内存中要创建相应的数据结构来描述目标文件。于是就有了file结构体。表示一个已经打开的文件对象。而进程执行open系统调用,所以必须让进程和文件关联起来。每个进程都有一个指针*files, 指向一张表files_struct,该表最重要的部分就是包涵一个指针数组,每个元素都是一个指向打开文件的指针!所以,本质上,文件描述符就是该数组的下标。所以,只要拿着文件描述符,就可以找到对应的文件。

6.文件描述符的分配规则

输出发现fd是3:

1    #include<iostream>
     2    #include<fcntl.h>
     3    #include<sys/stat.h>
     4    #include<sys/types.h>
     5    #include<unistd.h>
     6    int main()
     7    {
     8       int fd=open("myfile",O_RDONLY|O_CREAT);
     9       if(fd<0)
    10       {
    11           std::cout<<"文件打开失败!"<<std::endl;
    12       }
    13       std::cout<<fd<<std::endl;
    14       close(fd);
    15       return 0;
    16    }
12345678910111213141516


关闭0或者2,在看:

1    #include<iostream>
     2    #include<unistd.h>
     3    #include<fcntl.h>
     4    #include<sys/stat.h>
     5    #include<sys/types.h>
     6    int main()
     7    {
     8        close(0);
     9       // close(2);
    10        int fd=open("myfile",O_RDONLY|O_CREAT,0664);
    11        if(fd<0)
    12        {
    13            std::cout<<"文件打开失败!"<<std::endl;
    14            return 1;
    15        }
    16        std::cout<<"fd:"<<fd<<std::endl;
    17        close(fd);
    18        return 0;
    19    }
12345678910111213141516171819


发现是结果是: fd: 0 或者 fd 2 可见,文件描述符的分配规则:在files_struct数组当中,找到当前没有被使用的最小的一个下标,作为新的文件描述符。
----------------------最小未分配原则----------------------

7.重定向

1.清空重定向

我们发现第一次写的hello world被第二次写的你好呀覆盖了,这就是清空重定向。

2.追加重定向

我们发现第一次写的hello没有被第二次写的你覆盖,而是追加在hello后面,这就是追加重定向。

3.重定向本质

那如果关闭1呢?看代码:

1    #include<iostream>
     2    #include<stdlib.h>
     3    #include<unistd.h>
     4    #include<fcntl.h>
     5    #include<sys/types.h>
     6    #include<sys/stat.h>
     7    int main()
     8    {
     9        close(1);
    10        int fd=open("myfile",O_WRONLY|O_CREAT,0644);
    11        if(fd<0)
    12        {
    13            std::cout<<"文件打开失败!"<<std::endl;
    14            exit(1);
    15        }
    16        std::cout<<"fd:"<<fd<<std::endl;
    17        close(fd);
    18        return 0;
    19    }
12345678910111213141516171819


此时,我们发现,本来应该输出到显示器上的内容,输出到了文件 myfile 当中,其中,fd=1。这种现象叫做输出重定向。常见的重定向有:>, >>, <

  • [ ] 那重定向的本质是什么呢?

8.FILE

  • 因为IO相关函数与系统调用接口对应,并且库函数封装系统调用,所以本质上,访问文件都是通过fd访问的。
  • 所以C库当中的FILE结构体内部,必定封装了fd。

来段代码在研究一下:

1    #include<iostream>
     2    #include<fcntl.h>
     3    #include<sys/stat.h>
     4    #include<sys/types.h>
     5    #include<string.h>
     6    #include<unistd.h>
     7    #include<stdio.h>
     8    int main()
     9    {
    10        int fd=open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0644);
    11        if(fd<0)
    12        {
    13            std::cout<<"文件打开失败!"<<std::endl;
    14            return 1;
    15        }
    16        const char* str1="hello printf\n";
    17        const char* str2="hello fwrite\n";
    18        const char* str3="hello write\n";
    19        write(1,str3,strlen(str3));
    20        printf("%s\n",str1);
    21        fprintf(stdout,"%s\n",str2);
    22        fork();
    23        close(fd);
    24        return 0;
    25    
    26    
    27    }
123456789101112131415161718192021222324252627


但如果对进程实现输出重定向呢? ./hello > file , 我们发现结果变成了:

  • [ ] 我们发现 printf 和 fwrite (库函数)都输出了2次,而 write 只输出了一次(系统调用)。为什么呢?肯定和fork有关!
  • 一般C库函数写入文件时是全缓冲的,而写入显示器是行缓冲。
  • printf、fwrite库函数会自带缓冲区(进度条例子就可以说明),当发生重定向到普通文件时,数据的缓冲方式由行缓冲变成了全缓冲。
  • 而我们放在缓冲区中的数据,就不会被立即刷新,甚至fork之后。
  • 但是进程退出之后,会统一刷新,写入文件当中。
  • 但是fork的时候,父子数据会发生写时拷贝,所以当你父进程准备刷新的时候,子进程也就有了同样的一份数据,随即产生两份数据。
  • write 没有变化,说明没有所谓的缓冲。

综上: printf、fwrite 库函数会自带缓冲区,而 write 系统调用没有带缓冲区。另外,我们这里所说的缓冲区,都是用户级缓冲区。其实为了提升整机性能,OS也会提供相关内核级缓冲区,不过不再我们讨论范围之内。那这个缓冲区谁提供呢? printf fwrite 是库函数, write 是系统调用,库函数在系统调用的“上层”, 是对系统调用的“封装”,但是 write 没有缓冲区,而 printf fwrite 有,足以说明,该缓冲区是二次加上的,又因为是C,所以由C标准库提供。

如果有兴趣,可以看看FILE结构体:

typedef struct _IO_FILE FILE; 在/usr/include/stdio.h

在/usr/include/libio.h
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. /
define _IO_file_flags _flags
//缓冲区相关
/ The following pointers correspond to the C++ streambuf protocol. /
/ Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. /
char _IO_read_ptr; /* Current read pointer /
char _IO_read_end; /* End of get area. /
char _IO_read_base; /* Start of putback+get area. /
char _IO_write_base; /* Start of put area. /
char _IO_write_ptr; /* Current put pointer. /
char _IO_write_end; /* End of put area. /
char _IO_buf_base; /* Start of reserve area. /
char _IO_buf_end; /* End of reserve area. /
/ The following fields are used to support backing up and undo. */
char _IO_save_base; / Pointer to start of non-current get area. */
char _IO_backup_base; / Pointer to first valid character of backup area */
char _IO_save_end; / Pointer to end of non-current get area. /
struct _IO_marker _markers;
struct _IO_FILE _chain;
int _fileno; //封装的文件描述符
if 0
int _blksize;
else
int _flags2;
endif
_IO_off_t _old_offset; / This used to be _offset but it’s too small. /
define __HAVE_COLUMN / temporary /
/ 1+column number of pbase(); 0 is unknown. /
unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];
/ char _save_gptr; char _save_egptr; */
_IO_lock_t *_lock;
ifdef _IO_USE_OLD_IO_FILE
};

9.使用 dup2 系统调用


函数原型如下:

include <unistd.h>
int dup2(int oldfd, int newfd);

1    #include<iostream>
     2    #include<fcntl.h>
     3    #include<sys/stat.h>
     4    #include<sys/types.h>
     5    #include<string.h>
     6    #include<unistd.h>
     7    #include<stdio.h>
     8    int main()
     9    {
    10        int fd=open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0644);
    11        if(fd<0)
    12        {
    13            std::cout<<"文件打开失败!"<<std::endl;
    14            return 1;
    15        }
    16       
    17        dup2(fd,1);
            //close(fd);
    18        const char* str1="hello printf\n";
    19        const char* str2="hello fwrite\n";
    20        const char* str3="hello write\n";
    21        write(1,str3,strlen(str3));
    22        printf("%s\n",str1);
    23        fprintf(stdout,"%s\n",str2);
    24        fflush(stdout);
    25        close(fd);
    26        return 0;
    27    
    28    
    29    }
123456789101112131415161718192021222324252627282930

10.理解文件系统

我们使用ls -l的时候看到的除了看到文件名,还看到了文件元数据。

  • [ ] 每行包含7列:
  • 模式
  • 硬链接数
  • 文件所有者
  • 大小
  • 最后修改时间
  • 文件名

ls -l读取存储在磁盘上的文件信息,然后显示出来

其实这个信息除了通过这种方式来读取,还有一个stat命令能够看到更多信息

上面的执行结果有几个信息需要解释清楚:

inode:
为了能解释清楚inode我们先简单了解一下文件系统:

  • [ ] Linux ext2文件系统,上图为磁盘文件系统图(内核内存映像肯定有所不同),磁盘是典型的块设备,硬盘分区被划分为一个个的block。一个block的大小是由格式化的时候确定的,并且不可以更改。例如mke2fs的-b选项可以设定block大小为1024、2048或4096字节。而上图中启动块(Boot Block)的大小是确定的。
  • Block Group:ext2文件系统会根据分区的大小划分为数个Block Group。而每个Block Group都有着相同的结构组成。政府管理各区的例子。
  • 超级块(Super Block):存放文件系统本身的结构信息。记录的信息主要有:bolck 和 inode的总量,未使用的block和inode的数量,一个block和inode的大小,最近一次挂载的时间,最近一次写入数据的时间,最近一次检验磁盘的时间等其他文件系统的相关信息。Super Block的信息被破坏,可以说整个文件系统结构就被破坏了。
  • GDT,Group Descriptor Table:块组描述符,描述块组属性信息,有兴趣的同学可以在了解一下。
  • 块位图(Block Bitmap):Block Bitmap中记录着Data Block中哪个数据块已经被占用,哪个数据块没有被占用。
  • inode位图(inode Bitmap):每个bit表示一个inode是否空闲可用。
  • i节点表:存放文件属性 如 文件大小,所有者,最近修改时间等.。
  • 数据区:存放文件内容。

    inode编号是有限的,如果要增加inode编号,通过inode table。要找文件,先找文件inode,要找inode,先找inode ID。

将属性和数据分开存放的想法看起来很简单,但实际上是如何工作的呢?我们通过touch一个新文件来看看如何工作。

[root@localhost linux]# touch abc
[root@localhost linux]# ls -i abc
263466 abc

  • [ ] 创建一个新文件主要有一下4个操作:
  • 存储属性:内核先找到一个空闲的i节点(这里是263466)。内核把文件信息记录到其中。
  • 存储数据:该文件需要存储在三个磁盘块,内核找到了三个空闲块:300,500,800。将内核缓冲区的第一块数据复制到300,下一块复制到500,以此类推。
  • 记录分配情况:文件内容按顺序300,500,800存放。内核在inode上的磁盘分布区记录了上述块列表。
  • 添加文件名到目录。

新的文件名abc。linux如何在当前的目录中记录这个文件?内核将入口(263466,abc)添加到目录文件。文件名和inode之间的对应关系将文件名和文件的内容及属性连接起来。

11.理解硬链接

我们看到,真正找到磁盘上文件的并不是文件名,而是inode。 其实在linux中可以让多个文件名对应于同一个inode。

  • 1.txt和1.txtHard的链接状态完全相同,他们被称为指向文件的硬链接。内核记录了这个连接数,inode263466 的硬连接数为2。
  • 我们在删除文件时干了两件事情:1.在目录中将对应的记录删除,2.将硬连接数-1,如果为0,则将对应的磁盘释放。
  • 改变其中一个文件内容另一个文件内容也会跟着改变。

12.理解软连接


硬链接是通过inode引用另外一个文件,软链接是通过名字引用另外一个文件,在shell中的做法:

  • [ ] 下面解释一下文件的三个时间:
  • Access 最后访问时间。
  • Modify 文件内容最后修改时间。
  • Change 属性最后修改时间。

三、动态库和静态库

1.动态库和静态库

  • [ ] 静态库与动态库
  • 静态库(.a):程序在编译链接的时候把库的代码链接到可执行文件中。程序运行的时候将不再需要静态库。

    静态库比较大,是拷贝库的一份代码。

  • 动态库(.so):程序在运行的时候才去链接动态库的代码,多个程序共享使用库的代码。 一般默认生成的可执行程序都是动态的,动态库体积小,运行时加载,只有一份。
  • 一个与动态库链接的可执行文件仅仅包含它用到的函数入口地址的一个表,而不是外部函数所在目标文件的整个机器码。
  • 在可执行文件开始运行以前,外部函数的机器码由操作系统从磁盘上的该动态库中复制到内存中,这个过程称为动态链接(dynamic linking)。
  • 动态库可以在多个程序间共享,所以动态链接使得可执行文件更小,节省了磁盘空间。操作系统采用虚拟内存机制允许物理内存中的一份动态库被要用到该库的所有进程共用,节省了内存和磁盘空间。

动态链接生成的可执行程序比静态链接生成的可执行程序小的原因?

2.生成静态库


写出mylib.h代码:

#ifndef __MYLIB_H__
#define __MYLIB_H__ 
#include<stdio.h> 
void print();

#endif
123456

写出mylib.c代码:

#include"mylib.h"
void print()
{
    printf("Hello Linux!\n");
}
12345

写出main.c代码:

#include"mylib.h"
int main()
{
    print();
    return 0;
}
123456

生成静态库第一步,先生成目标文件: gcc -c mylib.c -o mylib.o

第二步,用目标文件生成静态库:ar -rc libmylib.a mylib.o
ar是gnu归档工具,rc表示(replace and create)

可以查看静态库中的目录列表:

t:列出静态库中的文件
v:verbose 详细信息
第三步运行main.c得到结果。 gcc main.c -L. -lmylib

-L 指定库路径
-l 指定库名
测试目标文件生成后,静态库删掉,程序照样可以运行。

3.生成动态库


写出mylib.h代码:

#ifndef __MYLIB_H__
#define __MYLIB_H__ 
#include<stdio.h> 
void print();

#endif
123456

写出mylib.c代码:

#include"mylib.h"
void print()
{
    printf("Hello Linux!\n");
}
12345

写出main.c代码:

#include"mylib.h"
int main()
{
    print();
    return 0;
}
123456

1.库搜索路径

  • 从左到右搜索-L指定的目录。
  • 由环境变量指定的目录 (LIBRARY_PATH)。
  • 由系统指定的目录。
  • /usr/lib
  • /usr/local/lib

生成动态库第一步,先生成目标文件: gcc -fPIC -c mylib.c

2.生成动态库

  • shared: 表示生成共享库格式
  • fPIC:产生位置无关码(position independent code)
  • 库名规则:libxxx.so

第二步,用目标文件生成动态库:gcc -shared -o libmylib.so *.o

3.使用动态库

  • [ ] 编译选项
  • I:链接动态库,只要库名即可(去掉lib以及版本号)。
  • L:链接库所在得路径。

第三步运行main.c得到结果。 gcc main.c -o main -L. -lmylib


总结

以上就是今天要讲的内容,本文仅仅简单介绍了进场基础IO的使用,而系统编程提供了大量能使我们快速便捷地处理数据的函数和方法,我们务必掌握。到这里,进程IO就结束了,后序将会有更重要的文章陆续更新,希望大家多多支持!另外如果上述有任何问题,请懂哥指教,不过没关系,主要是自己能坚持,更希望有一起学习的同学可以帮我指正,但是如果可以请温柔一点跟我讲,爱与和平是永远的主题,爱各位了。

————————————————
版权声明:本文为CSDN博主「森明帮大于黑虎帮」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_44918090/article/details/118185830

最后修改:2022 年 04 月 04 日
如果觉得我的文章对你有用,请随意赞赏