繁体   English   中英

如何使用c标准库获取目录大小

[英]How to get the directory size using c standard library

有没有办法使用c标准库函数获取目录的大小?

不可以.C和C ++标准库没有明确支持目录的概念。
就它们而言,“C:\\ test \\ test.txt”中的反斜杠没有特殊含义。 这是操作系统要处理的。

“目录大小”是什么意思?

  • 它是包含在此目录中的文件的总大小吗?
  • ...加上子目录的大小?
  • 它只链接到此目录中包含的文件数吗?
  • ...加上子目录的数量?
  • ...加上子目录本身的大小?

单个C库或系统调用都不可能实现这些功能。

这应该让你去。

请参阅此处查看完整计划: https//stackoverflow.com/questions/3948116/how-to-integrate-two-different-processes-together-using-two-different-programs-in/3953873#3953873

对于Windows,请参阅: http//code.google.com/p/portaputty/source/browse/trunk/windows/dirent.c?r = 8

或者: http//www.softagalleria.net/dirent.php

或者只使用MinGW编译器。

#include <unistd.h>
#include <dirent.h>
#include <sys/types.h> // for opendir(), readdir(), closedir()
#include <sys/stat.h> // for stat()


dir_proc = opendir(PROC_DIRECTORY) ;
    if (dir_proc == NULL)
    {
        perror("Couldn't open the " PROC_DIRECTORY " directory") ;
        return (pid_t) -2 ;
    }

    // Loop while not NULL
    while ( (de_DirEntity = readdir(dir_proc)) )
    {
        if (de_DirEntity->d_type == DT_DIR)
        {
            if (IsNumeric(de_DirEntity->d_name))
            {
                strcpy(chrarry_CommandLinePath, PROC_DIRECTORY) ;
                strcat(chrarry_CommandLinePath, de_DirEntity->d_name) ;
                strcat(chrarry_CommandLinePath, "/cmdline") ;
                FILE* fd_CmdLineFile = fopen (chrarry_CommandLinePath, "rt") ;  // open the file for reading text
                if (fd_CmdLineFile)
                {
                    fscanf(fd_CmdLineFile, "%s", chrarry_NameOfProcess) ; // read from /proc/<NR>/cmdline
                    fclose(fd_CmdLineFile);  // close the file prior to exiting the routine

                    if (strrchr(chrarry_NameOfProcess, '/'))
                        chrptr_StringToCompare = strrchr(chrarry_NameOfProcess, '/') +1 ;
                    else
                        chrptr_StringToCompare = chrarry_NameOfProcess ;

                    //printf("Process name: %s\n", chrarry_NameOfProcess);
                    //printf("Pure Process name: %s\n", chrptr_StringToCompare );

                    if ( CompareFunction(chrptr_StringToCompare, cchrptr_ProcessName, intCaseSensitiveness) )
                    {
                        pid_ProcessIdentifier = (pid_t) atoi(de_DirEntity->d_name) ;
                        closedir(dir_proc) ;
                        return pid_ProcessIdentifier ;
                    }
                }
            }
        }
    }
    closedir(dir_proc) ;

查看这篇文章,了解如何获取文件大小 您可能需要总结目录中文件的大小以获取“目录大小”。

如果您使用Linux,您可能会对这些帖子感兴趣:

  1. 如何在C中获取目录的大小?
  2. 通过C ???查找目录大小 LINUX ???

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM