简体   繁体   中英

How to get the directory size using c standard library

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

No. The C and C++ standard libraries do not explicitly support the concept of a directory.
As far as they are concerned, the backslashes in "C:\\test\\test.txt" have no special meaning. That is for the OS to handle.

What do you mean by the 'size of a directory'?

  • Is it the total size of the files contained into this directory?
  • ... plus the size of the sub-directories?
  • Is it only linked to the number of files contained in this directory?
  • ... plus the number of sub-directories?
  • ... plus the size of sub-directories themselves?

None of these are possible with a single C library or system call.

This should get you going.

See here for full program: https://stackoverflow.com/questions/3948116/how-to-integrate-two-different-processes-together-using-two-different-programs-in/3953873#3953873

For windows, see: http://code.google.com/p/portaputty/source/browse/trunk/windows/dirent.c?r=8

or this: http://www.softagalleria.net/dirent.php

or just use the MinGW compiler.

#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) ;

Check out this post with regard to how to get the size of a file . You may need to sum up the sizes of the files in a directory to get the "directory size".

If you are using Linux these posts may be of interest to you:

  1. How do I get the size of a directory in C?
  2. Find directory-size through C ??? LINUX ???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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