Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532930
  • 博文数量: 102
  • 博客积分: 3165
  • 博客等级: 中校
  • 技术积分: 1232
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-09 16:38
文章存档

2016年(1)

2013年(14)

2012年(6)

2011年(22)

2010年(57)

2009年(2)

我的朋友

分类: C/C++

2010-04-07 16:41:16

要养成良好的编程习惯,特别是使用一些设计到I/O操作的系统API(fopen,open,socket,bind,stat,opendir)进行编程时一定要对函数的执行结果状态进行判断。否则,当程序规模较大时,如果出现问题将很难进行定位。

简单如:

打开一个目录,并读取目录下一个文件的状态信息时,应如下编写代码

DIR *dp = NULL;
struct dirent  *dirEntry = NULL;
struct stat dirStat;

if((dp = opendir(dirName)) == NULL)
{
perror("opendir");
exit(1);
}

if((dirEntry = readdir(dp)) == NULL)
{
perror("readdir");
exit(2);
}

if(stat(dirEntry->d_name,&dirStat) < 0)
{
perror("stat");
exit(3);
}
阅读(2265) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~