段错误的问题

feixiang_aoyou 2006-09-29 10:09:12
在编译下面这段代码时提示if((dp=opendir(argv[1]))==NULL)这行中有段错误,用GDB调试也没有调试出来,我刚学linux没有多久,请各位帮忙看看是怎么一回事,谢谢了.
#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc!=2)
printf("a single argument is required");
if((dp=opendir(argv[1]))==NULL);
printf("can not open %s",argv[1]);
while((dirp=readdir(dp))!=NULL)
printf("%s\n",dirp->d_name);
closedir(dp);
exit(0);
}
...全文
122 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fytzzh 2006-09-30
  • 打赏
  • 举报
回复
还有:
if((dp=opendir(argv[1]))==NULL)
{
printf("can not open %s",argv[1]);
return -1;
}
fytzzh 2006-09-30
  • 打赏
  • 举报
回复
你的程序接收一个参数,如果你不传参数的话当然回dump掉。
修改一下:
#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
{
printf("a single argument is required\n");
return -1;
}
if((dp=opendir(argv[1]))==NULL);
printf("can not open %s",argv[1]);
while((dirp=readdir(dp))!=NULL)
printf("%s\n",dirp->d_name);
closedir(dp);
exit(0);
}
xfzhao_cn 2006-09-30
  • 打赏
  • 举报
回复
路过,学好GBB,到哪儿都不怕

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧