2010-11-03 47 views
2

是否有办法使用c标准库函数获取目录大小?如何使用c标准库获取目录大小

+0

请,请,如果你的意思是声明,“所有文件的总文件大小在此目录下“或”目录中的文件数量“。 – 2010-11-03 12:41:54

+2

目录列表是OS特定的。 – ruslik 2010-11-03 13:29:50

回答

2

你是什么意思的'目录的大小'?

  • 这是包含在这个目录中的文件的总大小吗?
  • ...再加上子目录的大小?
  • 它是否仅与此目录中包含的文件数量相关联?
  • ...加上子目录的数量?
  • ...加上子目录本身的大小?

这些对于单个C库或系统调用都是不可能的。

+0

+1。标准语言库不可能。 – DevSolar 2010-11-03 11:54:12

+0

与一个C库系统调用。 +1 – 2010-11-03 12:41:10

4

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

1

这应该让你去。

在这里看到完整的程序: 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) ; 
+0

wtf?和另外6个 – 2010-11-03 14:56:09

+0

@Matt Joiner:你是什么意思? – 2010-11-03 16:13:28