exit函数的妙用
来源:互联网 发布:it薪资排行 编辑:程序博客网 时间:2024/05/19 20:01
写了一个程序,用来判断一个文件是否存在:
#include<stdio.h>
main()
{
FILE *fp;
fp = fopen ("/home/wang/my/ct2.c", "r+");
if (fp == NULL)
{
printf("there is not this file!\n");
}
else
printf("the file is exist!\n");
fclose (fp);
}
r+表示为读写打开一个文本文件
如果在/home/wang/my/目录下的ct2.c文档存在,
则程序运行结果为:
the file is exist!
运行结果是正确的!
如果在/home/wang/my/目录下的ct2.c文档不存在,
则程序运行结果为:
there is not this file!
Segmentation fault (core dumped)
运行结果是错误的!
这时需要用到exit函数,将程序修改为如下形式:
#include<stdio.h>
main()
{
FILE *fp;
fp = fopen ("/home/wang/my/ct2.c", "r+");
if (fp == NULL)
{
printf("there is not this file!\n");
exit(0);
}
else
printf("the file is exist!\n");
fclose (fp);
}
程序运行结果为:
there is not this file!
运行结果是正确的!结果中没有了"Segmentation fault (core dumped)"!
exit函数的作用是关闭所有文件,终止正在执行的程序,待用户检查出错误,
修改后再运行。
- exit函数的妙用
- HYPERLINK函数的妙用
- 函数指针的妙用
- sscanf函数的妙用
- 函数索引的妙用
- extract()函数的妙用
- exit()函数的用法
- 绝对值函数Abs()的妙用
- 函数指针数组的妙用
- CreateProcess API函数的妙用
- ::GetModuleFileName函数的一个妙用
- ::GetModuleFileName函数的一个妙用
- 函数指针数组的妙用
- CreateProcess API函数的妙用
- 函数指针数组的妙用
- 函数指针数组的妙用
- 函数指针数组的妙用
- 函数指针数组的妙用
- HDOJ 题目1302 The Snail(模拟,水题)
- POJ-1944(枚举拆环)
- Android 动态显示和隐藏软键盘
- 第一轮 L
- Ogre输出屏幕2d文字(Overlay文字)
- exit函数的妙用
- py2exe使用教程(五)——指定额外文件
- Session简介,创建与销毁
- CC150:将一个矩阵旋转90度
- H264 pps sps作用
- 用数组和链表实现队列操作
- 接口于类型信息-类型信息
- Android ffmpeg H264 YUV显示
- DLL注入