Linux C 读取文件目录下的目录,并统计各目录下的文件数和目录数

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



int readFileList(char *basePath,int *count_dir,int *count_file)
{
    DIR *dir;
    struct dirent *ptr;
    char base[1000];

    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;
        else if(ptr->d_type == 8)    ///file
            (*count_file)++;
        else if(ptr->d_type == 10)    ///link file
            (*count_file)++;
        else if(ptr->d_type == 4)    ///dir
        {
            memset(base,'\0',sizeof(base));
            strcpy(base,basePath);
            strcat(base,"/");
            strcat(base,ptr->d_name);
            readFileList(base);
            (*count_dir)++;
        }
    }
    closedir(dir);
    return 1;
}

int main(void)
{
    DIR *dir;
    char basePath[1000];

    ///get the current absoulte path
    memset(basePath,'\0',sizeof(basePath));
    getcwd(basePath, 999);
    printf("the current dir is : %s\n",basePath);

    ///get the file list
    memset(basePath,'\0',sizeof(basePath));
    strcpy(basePath,"./XL");
    //************************
    DIR *dir;
    struct dirent *ptr;
    char base[1000];

    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }
    while ((ptr=readdir(dir)) != NULL)
    {
    	if(ptr->d_type == 4)    ///dir
       	{
       	    int count_dir=0;
			int count_file=0;
			int *p1=&count_dir;
			int *p2=&count_file;
            memset(base,'\0',sizeof(base));
            strcpy(base,basePath);
            strcat(base,"/");
            strcat(base,ptr->d_name);
            printf("the dir name: %s\n",basePath);
            readFileList(base,p1,p2);
            printf("the number of dir:%d\n",count_dir);
            printf("the number of file:%d\n",count_file);
            
         }
     }
    //************************
    //readFileList(basePath);
    //printf("the total is : %d\n",count_dir+count_file);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Linux 中,您可以使用 fork 函创建一个子进程,并使用 exec 函来运行 shell 命令 "ls" 和 "ls -a" 来统计指定目录下的文件和隐藏文件量。下面是一个示例代码: ``` #include <stdio.h> #include <unistd.h> #include <sys/wait.h> int main() { int total_files = 0; int hidden_files = 0; int fd[2]; if (pipe(fd) < 0) { perror("pipe"); return 1; } pid_t pid = fork(); if (pid < 0) { perror("fork"); return 1; } else if (pid == 0) { // child process close(fd[0]); dup2(fd[1], STDOUT_FILENO); execlp("ls", "ls", "-l", (char *)NULL); } else { // parent process close(fd[1]); wait(NULL); char buffer[1024]; while (read(fd[0], buffer, sizeof buffer) > 0) { total_files++; if (buffer[0] == '.') { hidden_files++; } } printf("Total files: %d\n", total_files); printf("Hidden files: %d\n", hidden_files); } return 0; } ``` 该代码实现了创建一个管道,在子进程中运行 "ls -l" 命令,并在父进程中读取命令的输出,最后计算文件和隐藏文件量并打印出来。 ### 回答2: 要编写一个Linux子进程来统计指定目录下的文件及隐藏文件量,我们可以使用C语言编写一个简单的程序。以下是实现这个功能的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include <unistd.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("请指定一个目录作为参\n"); exit(1); } DIR *dir; struct dirent *entry; int file_count = 0; int hidden_count = 0; dir = opendir(argv[1]); if (dir == NULL) { printf("无法打开目录\n"); exit(1); } while ((entry = readdir(dir)) != NULL) { // 忽略目录本身和上一级目录 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } file_count++; // 根据文件名第一个字符是否为.判断是否为隐藏文件 if (entry->d_name[0] == '.') { hidden_count++; } } closedir(dir); printf("总文件:%d\n", file_count); printf("隐藏文件:%d\n", hidden_count); return 0; } ``` 这个程序首先会检查用户是否提供了一个目录作为参,然后打开该目录并读取其所有项。在读取每个项时,会将计器增加1,并检查文件名的第一个字符是否为点,如果是则将隐藏文件的计器增加1。最后,程序会输出文件和隐藏文件量。 ### 回答3: 要编写一个Linux的子进程来统计指定目录下的文件和隐藏文件量,可以使用C语言的系统调用来实现。 首先,需要使用fork()函创建一个子进程。接下来,在子进程中使用chdir()函将当前的工作目录切换到指定的目录下。 然后,使用opendir()函打开指定目录,并使用readdir()函循环读取目录中的每个文件。对于每个读取到的文件,可以使用stat()函获取文件的详细信息。其中,可以通过st_mode成员来判断文件是否为隐藏文件。如果st_mode的低位是S_IFDIR,表示该文件目录;而隐藏文件文件名通常以点开头,可以通过判断文件名的第一个字符是否是点来确定是否为隐藏文件。 在循环中,可以使用一个变量来记录文件的总,并通过一个条件语句判断是否为隐藏文件,如果是隐藏文件,则相应的计器自增。 最后,可以使用printf()函输出文件和隐藏文件量,并使用closedir()函关闭目录。 整个子进程的实现可以放在一个函中,然后在主函中调用该函即可。 代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <sys/types.h> #include <sys/stat.h> void countFiles(const char *dir) { DIR *dp; struct dirent *entry; struct stat statbuf; int fileCount = 0; int hiddenCount = 0; if ((dp = opendir(dir)) == NULL) { fprintf(stderr, "Cannot open directory: %s\n", dir); return; } chdir(dir); while ((entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); if (S_ISDIR(statbuf.st_mode)) { continue; } fileCount++; if (entry->d_name[0] == '.') { hiddenCount++; } } printf("总文件:%d\n", fileCount); printf("隐藏文件:%d\n", hiddenCount); closedir(dp); } int main() { const char *dir = "/path/to/directory"; countFiles(dir); return 0; } ``` 需要将"/path/to/directory"替换为指定的目录路径。编译运行该代码后,即可在终端中看到指定目录文件的总和隐藏文件量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值