Linux文件和I/O

版权声明:转载请注明出处 https://blog.csdn.net/qq_27350133/article/details/84289093

文件基础

文件即一组相关数据的有序集合。

文件类型

  • 常规文件 r
  • 目录文件(文件夹) d (directory)
  • 字符设备文件 c (char) 代表一个字符设备
  • 块设备文件 b (block)
  • 管道文件 p 进程间通信的一种机制
  • 套接字文件 s 进程间通信和网络通信的一种机制
  • 符号链接文件 l (link) 相当于windows的快捷方式

在linnux中,操作设备也就是操作相关文件。

标准I/O

标准I/O也就是C库 提供的一组操作操作标准I/O的函数。实用方便,可移植性高。
其内部实现了缓冲机制减少了系统调用。

系统调用

裸机编程时(无操作系统)代码直接通过修改寄存器和硬件打交道。在linux操作系统中,由于一般会同时运行多个应用程序,直接访问硬件可能会导致系统硬件之间的冲突,比如 两个应用程序在同一时刻都要访问蜂鸣器,这是就会产生冲突,于是乎需要OS来调节冲突,所以应用层代码不直接和硬件打交道,而是通过一套系统函数接口和硬件打交道。这样就可以保证系统的安全。这就是系统调用。
不同的操作系统有不同的接口,所以不同平台的代码往往不通用。

缓冲机制

举例:无缓冲:应用程序在每次需要磁盘数据时,每次都需要通过系统调用去访问磁盘。
有缓冲:应用程序去读磁盘时,比如需要10个数据,系统有可能会将100个甚至更多数据放在系统自动开辟的缓冲区中,在程序下一次需要访问磁盘读数据时就不需要再经过系统调用了。在向磁盘写入数据时,只有当缓冲区写满了,才会通过系统调用向磁盘写数据。
这样就提高了系统的效率。

即FILE结构体,一个FILE结构体代表一个打开的文件。标准I/O操作都围绕流来展开。
linux中为二进制流 :一个换行就是一个 \n

缓冲类型

全缓冲

参考缓冲机制。

行缓冲

遇见一个\n或者缓冲区满才会输出到I/O设备。

无缓冲

直接输入输出。

当我们打开一个文件时,系统自动打开了三个流:分别是标准输入流,标准输出流,标准错误流。每个流代表一种文件描述符。

打开流


 #include <stdio.h> //需要的头文件
       FILE *fopen(const char *path, const char *mode);

参数:path

文件绝对路径。

参数:mode

参考man手册: man 3 fopen
The argument mode points to a string beginning with one of the follow‐
ing sequences (Additional characters may follow these sequences.):

   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  cre‐
          ated  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.

关闭流

流后使用要及时关闭。当关闭流时会自动刷新缓冲区中的数据并释放缓冲区。
流关闭后不能进行任何操作,否则将导致不可预知的后果。

 #include <stdio.h>
 int fclose(FILE *fp);

fp为打开文件返回的FILE* 。

利用标准I/O拷贝文件举例

代码里含有详细注释。
实现方法为 :以只读方式打开源文件,以写入方式打开目标文件,若没有目标文件将会自动创建。然后利用fgetc从源文件中将字符挨个读出,用fputc写入目标文件,知道文件末尾。

#include <stdio.h>
int main(int argc ,char *argv[])
{
	FILE *fps,*fpd; //定义源文件和目标文件结构体
	int ch;
	if(argc<3)
	{
		printf("Usage: %s <dst_file> <src_file>\n",argv[0]);
		return -1;
	}

	if((fpd = fopen(argv[1],"w"))==NULL) //以可写方式打开文件或新建文件
	{
		perror("open file faild!\n");
		return -1;
	}
	if((fps = fopen(argv[2],"r"))==NULL)//以只读方式打开文件
	{
		perror("open src_file faild!\n");
		return -1;
	}
	while( (ch = fgetc(fps)) != EOF)//文件末尾将会返回EOF 
	{
		fputc(ch,fpd);//输出到目标文件
	}
	fclose(fps);//操作完文件后马上关闭
	fclose(fpd);
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_27350133/article/details/84289093
今日推荐