Is there is way to get the size of a directory using c standard library functions?
4 Answers
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.
- 15,488
 - 4
 - 32
 - 41
 
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.
- 36,376
 - 13
 - 83
 - 122
 
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) ;
- 1
 - 1
 
- 78,642
 - 66
 - 377
 - 442
 
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: