This commit is contained in:
hairrrrr
2020-04-22 16:35:45 +08:00
parent 3f85fd21a3
commit 36984b86f5
2 changed files with 41 additions and 0 deletions

View 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) ...

View File

@@ -0,0 +1,15 @@
下面给出了一个程序的框架。此程序打开文件 example.dat 进行读操作,并检查打开是否成功,让后在程序终止前把文件关闭:
可以将 fp 的声明与函数调用结合:
```c
FILE* fp = fopen(FILE_NAME, "r");
```
还可以将函数调用与 NULL 判定结合:
```c
if((fp = fopen(FILE_NAME, "r")) == NULL) ...
```