diff --git a/Coding/Examples/22 输入&输出/01 fopen 使用框架/frame.c b/Coding/Examples/22 输入&输出/01 fopen 使用框架/frame.c new file mode 100644 index 0000000..501cdc8 --- /dev/null +++ b/Coding/Examples/22 输入&输出/01 fopen 使用框架/frame.c @@ -0,0 +1,26 @@ +#define _CRT_SECURE_NO_WARNINGS 1 + +#include +#include + +#define FILE_NAME "example.dat" + +int main(void) { + + FILE* fp; + + fp = fopen(FILE_NAME, "r"); + if (fp == NULL) { + printf("Can't open %s\n", FILE_NAME); + exit(EXIT_FAILURE); + } + + // ִв + + fclose(fp); + + return 0; +} + +// FILE* fp = fopen(FILE_NAME, "r"); +// if((fp = fopen(FILE_NAME, "r")) == NULL) ... diff --git a/Coding/Examples/22 输入&输出/01 fopen 使用框架/readme.md b/Coding/Examples/22 输入&输出/01 fopen 使用框架/readme.md new file mode 100644 index 0000000..d9d0aa5 --- /dev/null +++ b/Coding/Examples/22 输入&输出/01 fopen 使用框架/readme.md @@ -0,0 +1,15 @@ +下面给出了一个程序的框架。此程序打开文件 example.dat 进行读操作,并检查打开是否成功,让后在程序终止前把文件关闭: + +可以将 fp 的声明与函数调用结合: + +```c +FILE* fp = fopen(FILE_NAME, "r"); +``` + +还可以将函数调用与 NULL 判定结合: + +```c +if((fp = fopen(FILE_NAME, "r")) == NULL) ... +``` + +