Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C在目录中迭代并在特定级别停止?_C_Process_Directory - Fatal编程技术网

如何使用C在目录中迭代并在特定级别停止?

如何使用C在目录中迭代并在特定级别停止?,c,process,directory,C,Process,Directory,我试图在终端中打印系统中当前所有进程的名称。为此,我必须进入“proc”目录中以进程ID命名的所有目录。因此,我一直循环到“acpi”目录之前,并尝试读取每个进程目录中的状态文件。但我不太明白如何读取目录中的文件。在运行下面的代码时: #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <stdlib.h> #include <dirent.h>

我试图在终端中打印系统中当前所有进程的名称。为此,我必须进入“proc”目录中以进程ID命名的所有目录。因此,我一直循环到“acpi”目录之前,并尝试读取每个进程目录中的状态文件。但我不太明白如何读取目录中的文件。在运行下面的代码时:

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, const char *argv[])
{

DIR* FD;
struct dirent* in_file;
FILE  *process_file;
char ch, pname[1024];
int i=0;

FD = opendir ("/proc");

while ((in_file = readdir(FD))) 
{
    if (strcmp (in_file->d_name, "acpi") == 0)
        break;

    else 
    {
        opendir(in_file->d_name);

        process_file = fopen("status", "r");

        while( ( ch = fgetc(process_file) ) != '\n' )
        {
            pname[i] = ch;
            i++;
        } 
        printf(" %s \n",pname);
        fclose(process_file);


        closedir(in_file->d_name);
    }

}

closedir(FD);
return 0;

}

要解决此错误,请保存打开的目录指针。然后用它来关闭目录

DIR*process\u DIR=opendir(在文件->文件名中)


closedir(过程目录)

这是一个关于何时使用递归函数的好例子

该函数将获取一个目录名,打开该目录,并循环遍历结果。对于不是
.
的每个结果,调用
stat
函数获取每个条目的状态。然后在文件模式下使用
S_ISREG
S_ISDIR
宏查看它是常规文件还是目录。如果它是一个目录,则从父目录和刚找到的目录中构建一个新字符串,并将其传递给递归函数调用

因此,函数将如下所示:

void processDirectory(char dirname[])
{
    struct stat statbuf;
    DIR *dir;
    struct dirent *de;
    char *subdirname;
    int rval, ;

    if ((dir = opendir(dirname)) == NULL) {
        perror("Failed to open directory %s", dirname);
        exit(1);
    }
    while ((errno = 0, de = readdir(dir)) != NULL) {
        rval = stat(de->d_name, &statbuf);
        if (rval == -1) {
            perror("stat failed"); 
            exit(1);
        }

        if (S_ISREG(statbuf.st_mode)) {
            // process as a regular file
        } else if (S_ISDIR(statbuf.st_mode)) {
            if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
                subdirname = malloc(strlen(dirname) + strlen(de->d_name) + 2);
                if (subdirname == NULL) {
                    perror("malloc failed");
                    exit(1);
                }
                strcpy(subdirname, dirname);
                strcat(subdirname, "/");
                strcat(subdirname, de->d_name);
                processDirectory(subdirname);
                free(subdirname);
            }
        }
    }
    if (errno && (errno != ENOENT)) {
        perror("Failed to read directory %s", dirname);
        exit(1);
    }
    closedir(dir);

}

closedir
采用的是
DIR*
而不是
const char*
请澄清您的问题,您在标题中提到的问题还是您收到的错误?FD是我的DIR*。我不确定我所采用的在目录中打开目录的方法是否正确。我的问题是标题中提到的。@dothermitian-您可能应该查阅手册页,手册页上非常清楚地说明了这一点,这很有帮助。谢谢,没问题。如果您觉得它有用,您可以免费使用。
void processDirectory(char dirname[])
{
    struct stat statbuf;
    DIR *dir;
    struct dirent *de;
    char *subdirname;
    int rval, ;

    if ((dir = opendir(dirname)) == NULL) {
        perror("Failed to open directory %s", dirname);
        exit(1);
    }
    while ((errno = 0, de = readdir(dir)) != NULL) {
        rval = stat(de->d_name, &statbuf);
        if (rval == -1) {
            perror("stat failed"); 
            exit(1);
        }

        if (S_ISREG(statbuf.st_mode)) {
            // process as a regular file
        } else if (S_ISDIR(statbuf.st_mode)) {
            if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
                subdirname = malloc(strlen(dirname) + strlen(de->d_name) + 2);
                if (subdirname == NULL) {
                    perror("malloc failed");
                    exit(1);
                }
                strcpy(subdirname, dirname);
                strcat(subdirname, "/");
                strcat(subdirname, de->d_name);
                processDirectory(subdirname);
                free(subdirname);
            }
        }
    }
    if (errno && (errno != ENOENT)) {
        perror("Failed to read directory %s", dirname);
        exit(1);
    }
    closedir(dir);

}