为什么要设计标准 I/O 库?

  1. 直接使用 API 进行文件访问时,需要考虑许多细节问题,例如:read 、 write 时,缓冲区的大小该如何确定,才能使效率最优
  2. read 和 write 等底层系统调用函数进行输入输出时,在用户态内核态之间来回切换,每次读出或写入的数据量较少,导致频繁的 I/O 操作,增加了系统开销

标准 I/O 库是 ANSI C 规范的一部分,函数原型在文件 stdio.h中定义,对底层 I/O 系统

调用进行了封装,为程序员提供了带有格式转换功能的输入输出操作,并在用户空间

增加了缓冲区管理

  • 分离了应用程序空间和实际的物理设备
  • 减少了直接读盘次数,提高性能
  1. 回给应用程序
  2. writes )),

完为止;

序就完全不需要等到数据

可以了。

fopen() 函数

    Linux 标准 I/O 库

Linux 标准 I/O 库

    Linux 标准 I/O 库

fdopen() 函数

    Linux 标准 I/O 库

Linux 标准 I/O 库

// fdopen 函数示例
FILE *fp;
int fd;
if ((fp = fopen ("hello.txt", "w+")) == NULL)
	printf("fopen file error\n");
	return 0;}
fprintf(fp , "hello word\n");
fclose(fp);
if ((fd = open("hello.txt", O_RDWR )) == 1) {
	printf("open file fail\n");
	return 0;}
if ((fp = fdopen( fd , "a+")) == NULL)
	printf("fdopen open\n");
	return 0;
}
fprintf(fp , "linux c program");
fclose(fp);

相关文章:

  • 2022-12-23
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2022-02-12
  • 2021-10-09
  • 2021-04-22
猜你喜欢
  • 2021-04-30
  • 2021-09-04
  • 2021-08-16
  • 2021-06-03
  • 2022-12-23
  • 2021-09-25
相关资源
相似解决方案