2010-11-03 37 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