Chinaunix首页 | 论坛 | 博客
  • 博客访问: 116632
  • 博文数量: 29
  • 博客积分: 1215
  • 博客等级: 中尉
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-05 16:29
文章分类
文章存档

2010年(29)

我的朋友

分类: C/C++

2010-12-05 20:32:05

本程序主要练习使用函数opendir,readdir,closedir对目录进行处理,实现读取目录中所有文件的功能。
函数清单如下:


#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc,char *argv[])
{
    DIR *dp;
    struct dirent *dirp;

    if(argc != 2)
    {
        perror("the number of arguments is wrong!");
        exit(1);
    }
    if((dp = opendir(argv[1])) == NULL)
    {
        printf("Can't open %s\n",argv[1]);
        exit(1);
    }
    while((dirp = readdir(dp)) != NULL)
    {
         printf("%s\n",dirp->d_name);
    }
    closedir(dp);
    exit(0);
}


编译源文件:
    gcc -o listfile listfile.c
执行文件:
    ./listfile ~/eagle
输出结果:
    Can't open /home/longmenyu/eagle
执行文件:
     ./listfile ~/test/code
输出结果:
listfile
hello
mythread
.
..
main_function
执行文件:
    ./listfile /home /home/longmenyu/test
输出结果:
the number of arguments is wrong!: Success



阅读(834) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-07 10:02:14

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com