mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-02-09 05:33:18 +08:00
4-22
This commit is contained in:
26
Coding/Examples/22 输入&输出/01 fopen 使用框架/frame.c
Normal file
26
Coding/Examples/22 输入&输出/01 fopen 使用框架/frame.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
#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) ...
|
||||
15
Coding/Examples/22 输入&输出/01 fopen 使用框架/readme.md
Normal file
15
Coding/Examples/22 输入&输出/01 fopen 使用框架/readme.md
Normal file
@@ -0,0 +1,15 @@
|
||||
下面给出了一个程序的框架。此程序打开文件 example.dat 进行读操作,并检查打开是否成功,让后在程序终止前把文件关闭:
|
||||
|
||||
可以将 fp 的声明与函数调用结合:
|
||||
|
||||
```c
|
||||
FILE* fp = fopen(FILE_NAME, "r");
|
||||
```
|
||||
|
||||
还可以将函数调用与 NULL 判定结合:
|
||||
|
||||
```c
|
||||
if((fp = fopen(FILE_NAME, "r")) == NULL) ...
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user