我怎样才能得到这个readdir代码示例来搜索其他目录 [英] how can I get this readdir code sample to search other directories

查看:120
本文介绍了我怎样才能得到这个readdir代码示例来搜索其他目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用一个代码示例,最初设计为采用一个参数,然后在当前目录中搜索该参数,我试图通过替换另一个目录(/ dev / shm) 。用/ dev / shm,但是当我搜索某些东西*(注意通配符)时,代码什么也没有变。通配符搜索在当前目录中工作正常,所以我不认为这是通配符问题,如果有人可以帮助我,但我真的很感激,谢谢!

  #include< dirent.h> 
#include< errno.h>
#include< stdio.h>
#include< string.h>


static void lookup(const char * arg)
{
DIR * dirp;
struct dirent * dp;


if((dirp = opendir(。))== NULL){
perror(could not open'。');
return;
}


do {
errno = 0;
if((dp = readdir(dirp))!= NULL){
if(strcmp(dp-> d_name,arg)!= 0)
continue;


(void)printf(found%s\\\
,arg);
(void)closedir(dirp);
return;


$ b} while(dp!= NULL);


if(errno!= 0)
perror(error reading directory);
else
(void)printf(找不到%s\\\
,arg);
(void)closedir(dirp);
return;


$ b int main(int argc,char * argv [])
{
int i;
for(i = 1; i< argc; i ++)
lookup(argv [i]);
return(0);

$ / code>


解决方案

opendir 不处理通配符。它期望一个真实的目录路径。我不确定你说的是什么意思当前目录中的
$ b


通配符搜索

blockquote>

如果你的意思是它在你的shell中工作,这是可以预料的。 shell将首先展开通配符,然后执行你输入的命令。

那么如何解决这个问题呢?在调用 opendir 之前,使用 glob 自己扩展通配符。






编辑:对不起,我以为你试图匹配目录名称中的通配符。它看起来像你想使用通配符匹配目录内容。如果(strcmp(dp-> d_name,arg)!= 0)
<$ p
$ b

  / code> 

with

  if (fnmatch(arg,dp-> d_name,0)!= 0)






您也可以使用 glob 来做这件事。它实际上将取代对 opendir 和循环的调用。下面是一个使用 glob 的例子:
$ b

  #include  #include< stdio.h> 


static void lookup(const char * root,const char * arg)
{
size_t n;
glob_t res;
char ** p;

chdir(root);
glob(arg,0,0,& res);

n = res.gl_pathc;
if(n <1){
printf(找不到%s \ n,arg);
} else {
for(p = res.gl_pathv; n; p ++,n---){
printf(found%s \ n,* p);
}
}
globfree(& res);


$ b int main(int argc,char * argv [])
{
int i;
for(i = 2; i lookup(argv [1],argv [i]);
return(0);
}


I am currently working with a code example that initially is designed to take an argument, then search for that argument in the current directory, I've tried to make it search another directory (/dev/shm to exact) by replacing the "." with "/dev/shm" but the code turns up nothing when i search for something* (notice the wildcard). The wild card search works fine in the current directory so I do not think it is the wild card that is the problem, If someone could help me out though I would really appreciate it, thanks!

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>


static void lookup(const char *arg)
{
    DIR *dirp;
    struct dirent *dp;


    if ((dirp = opendir(".")) == NULL) {
        perror("couldn't open '.'");
        return;
    }


    do {
        errno = 0;
        if ((dp = readdir(dirp)) != NULL) {
            if (strcmp(dp->d_name, arg) != 0)
                continue;


            (void) printf("found %s\n", arg);
            (void) closedir(dirp);
                return;


        }
    } while (dp != NULL);


    if (errno != 0)
        perror("error reading directory");
    else
        (void) printf("failed to find %s\n", arg);
    (void) closedir(dirp);
    return;
}


int main(int argc, char *argv[])
{
    int i;
    for (i = 1; i < argc; i++)
        lookup(argv[i]);
    return (0);
}

解决方案

opendir doesn't handle wildcards. It expects a real directory path. I'm not sure what you mean when you say

wildcard search works in the current directory

If you mean it works in your shell, that's to be expected. The shell will first expand the wildcard and then perform the command you typed.

So how to solve this? Expand the wildcard yourself using glob before calling opendir.


Edit: sorry, I thought you were trying to match the wildcard in the directory name. It looks like you want to match directory contents using the wildcard. In that case simply replace

if (strcmp(dp->d_name, arg) != 0)

with

if (fnmatch(arg, dp->d_name, 0) != 0)


You could also use glob for this. It will actually replace the call to opendir and the loop. Here is an example for using glob:

#include <glob.h>
#include <stdio.h>


static void lookup(const char *root, const char *arg)
{
    size_t n;
    glob_t res;
    char **p;

    chdir(root);
    glob(arg, 0, 0, &res);

    n = res.gl_pathc;
    if (n < 1) {
        printf("failed to find %s\n", arg);
    } else {
        for (p = res.gl_pathv; n; p++, n--) {
            printf("found %s\n", *p);
        }
    }
    globfree(&res);
}


int main(int argc, char *argv[])
{
    int i;
    for (i = 2; i < argc; i++)
        lookup(argv[1], argv[i]);
    return (0);
}

这篇关于我怎样才能得到这个readdir代码示例来搜索其他目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆