|
fclose 函数文档
函数概要:
fclose 函数用于关闭先前由 fopen 函数打开的文件。
fclose 函数会将缓冲区内的数据写入文件中,并释放系统所提供的文件资源。
函数原型:
- #include <stdio.h>
- ...
- int fclose(FILE *fp);
[color=rgb(51, 102, 153) !important]复制代码
参数解析:
返回值:
1. 如果文件关闭成功,返回值是 0;
2. 如果文件关闭失败,返回值是 EOF,并设置 errno 为指定的错误。
备注:
1. 磁盘已满、设备出错或者 I/O 错误均可能导致 fclose 函数调用失败。
演示:
- #include <stdio.h>
- #include <stdlib.h>
- int main(void)
- {
- FILE *fp;
- int ch;
- if ((fp = fopen("hello.txt", "r")) == NULL)
- {
- printf("打开文件失败!\n");
- exit(EXIT_FAILURE);
- }
- while ((ch = getc(fp)) != EOF)
- {
- putchar(ch);
- }
- fclose(fp);
- return 0;
- }
[color=rgb(51, 102, 153) !important]复制代码
|
|