| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 479 人关注过本帖
标题:一个类似于find的简单程序实现 大牛请进!!!!!!!!!!
只看楼主 加入收藏
黄色海岸2009
Rank: 1
等 级:新手上路
帖 子:38
专家分:2
注 册:2009-11-7
结帖率:87.5%
收藏
已结贴  问题点数:20 回复次数:2 
一个类似于find的简单程序实现 大牛请进!!!!!!!!!!
我是想编一个类似于函数find的简单程序 最后是要以这样的形式运行
。/crawl path... [-maxdepth n] [-name pattern] [-type {d,f}] [-size [+-]n]
下面是我的程序,size的部分我不知道怎么编。希望大家不吝赐教!!!1
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<fnmatch.h>
#include<string.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
static int maxdepth;
static char *pattern;
static int size;
static char *path;
static int type;
readline(int argc,char *argv[]){
    int i;
    if (argc<=1)
    {
        printf("usage: myfind path... [-maxdepth n] [-name pattern] [-type {d,f}] [-size [+-] n]\n");
    return ;
    }
    else if (argc>=2)
    {
        for (i=1;i<argc;i++)
        {
            if (strcmp(argv[i],"-maxdepth")==0 && strlen(argv[i+1])>0)
            {
                int depth = atoi(argv[i+1]);
                if (depth>=0)
                {
            maxdepth=depth;
                }
                else return ;
            }
            else if (strcmp(argv[i],"-maxdepth")==0 && strlen(argv[i+1])==0)
            {
                return ;
            }
            if (strcmp(argv[i],"-name")==0 && strlen(argv[i+1])>0)
  {
                pattern = argv[i+1];
            }
            else if (strcmp(argv[i],"-name")==0 && strlen(argv[i+1])==0)
            {
                return ;
            }
            if (strcmp(argv[i],"-type")==0 && strlen(argv[i+1])==1)
            {
                if (*argv[i+1]=='f')
                {
                        type=0;
                }
                else if (*argv[i+1]=='d')
                {
                type=1;
                }
            }
            else if (strcmp(argv[i],"-type")==0&&strlen(argv[i+1])==0)
            {
                return ;
            }
            }
     }
    return;
}

void find_target(char *dir_name,char *target,int depth){
        char str[1024];
        DIR *dir_ptr;
        struct dirent *dirent_ptr;
        int zeichen;
    int status;
        const char *temp;
        int temp_depth;
    struct stat file_info;
        if(stat(str,&file_info)!=-1){
                if(S_ISDIR(file_info.st_mode))
                        status=1;
        }
        else status=0;
        if((dir_ptr=opendir(dir_name))!=NULL){
   while((dirent_ptr=readdir(dir_ptr))!=NULL){
                        if(strcmp(dirent_ptr->d_name,".")!=0&&strcmp(dirent_ptr->d_name,"..")!=0){
                                strcpy(str,dir_name);
                                strcat(str,"/");
                                strcat(str,dirent_ptr->d_name);
                                strcat(str,"\0");
                                temp=strrchr(str,'/');
                                if(temp==NULL) temp=str;
                                else temp++;
                                zeichen=fnmatch(target,temp,FNM_PATHNAME|FNM_PERIOD);
                                if(zeichen!=FNM_NOMATCH){
                        
                                        printf("%s\n",str);
                                        temp_depth++;
                                }
                              
                if(status<depth)
                                        find_target(str,target,depth);
                        }
                }
                closedir(dir_ptr);
        }
        else {
                fprintf(stderr,"can not open the file%s:",dir_name);
                perror("");
        }
}
main(int argc,char *argv[]){
        int i,temp;
        for(i=1;i<argc;i++){
                if(argv[i][0]=='-') break;
                }
        if (!(readline(argc, argv))){
                exit(EXIT_FAILURE);
        }
         if(i<2)
                  find_target(".", pattern, maxdepth);
     else{
      for (temp=1; temp<i; temp++){
                         find_target(argv[temp], pattern, maxdepth);
                }
}
         return ;
         }
搜索更多相关主题的帖子: 牛请进 find 
2010-07-14 14:57
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:20 
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);

or you can use stat

struct stat st;
stat(filename, &st);
size = st.st_size;


It may help you.


[ 本帖最后由 Devil_W 于 2010-7-14 15:26 编辑 ]
2010-07-14 15:23
黄色海岸2009
Rank: 1
等 级:新手上路
帖 子:38
专家分:2
注 册:2009-11-7
收藏
得分:0 
OK.我试试看
2010-07-14 21:33
快速回复:一个类似于find的简单程序实现 大牛请进!!!!!!!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020008 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved