fopen fclose 函數練習
程序代碼:
#include <stdio.h> #include <stdlib.h> int main() { FILE* fp = NULL; int a = 0; fp = fopen("data.txt","rt"); //打開文件 if(fp == NULL) { perror("data.txt"); exit(-1); } else { printf("succeed in opening the file!/n"); } fclose(fp); //關閉文件 return 0; }
運行結果:
1、文件存在
$ ./test succeed in opening the file!
2、文件不存在
$ ./test data.txt: No such file or directory
最後更新:2017-04-02 06:51:36