The simplest way to count the size (number of bits) of a directory is to enter the command in the console:
DU-SB Directory Address
implementing this function in C + + is achieved by recursively traversing files and subdirectories in the directory. It is important to note that because the byte count is too large, only one integer is counted for the number of bytes, and a larger directory will overflow. So I used TB, GB, MB, KB, and byte five levels to represent the size of a directory.
My code is as follows:
#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <dirent.h> #include <string.h># define bytes_of_current_folder 4096class checkspace{public://Constructor CheckSpace (char *filepath ) {THIS&NBSP;->&NBSP;M_TB&NBSP;=&NBSP;0;THIS&NBSP;->&NBSP;M_GB&NBSP;=&NBSP;0;THIS&NBSP;->&NBSP;M_MB = 0;this -> m_kb = 0;this -> m_bytes = 0;strcpy (This -> m_filepath, filepath); Check (filepath); //the size of the file in the statistics directory Addbytes (4096), //plus the 4096}//of the directory itself gets the properties INT&NBSP;GETTB () &NBSP;{&NBSP;RETURN&NBSP;THIS&NBSP;->&NBSP;M_TB;&NBSP;}INT&NBSP;GETGB () { return this - >&NBSP;M_GB;&NBSP;}INT&NBSP;GETMB () { return this -> m_mb; }int getkb () { return this -> m_kb; }int getbytes () &NBSP;{&Nbsp;return this -> m_bytes; }//Show Content Void display () {printf ("Query directory path %s\n", M_filepath);p rintf ("Occupied space %dtb %dgb %dmb %dkb %dbyte (s) \ n", M_TB,&NBSP;M_GB,&NBSP;M_MB, m_kb, m_bytes);} private:int m_tb; //tbint m_gb; //gbint m_mb; //mbint m_kb; //kbint m_bytes; //bytechar m _filepath[128]; //Directory address//byte number increased (auto carry) void addbytes (int bytes) {M_bytes += bytes;while (m_bytes >= 1024) {m_bytes -= 1024;m_kb++;} while (m_kb >= 1024) {m_kb -= 1024;m_mb++;} while (m_mb >= 1024) {m_mb -= 1024;m_gb++;} while (m_gb >= 1024) {m_gb -= 1024;m_tb++;}} View the amount of space that a directory occupies (excluding 4096Byte of the directory itself) Void check (Char *dir) {dir *dp;struct dirent *entry; struct stat statbuf;if ((Dp = opendir (dir)) == null) {fprintf (stderr, "cannot open dir: %s\n", dir); exit (0);} ChDir (dir);while ((Entry = readdir (DP)) != null) {Lstat (Entry -> d_name, &NBSP;&STATBUF);if (S_isdir (Statbuf.st_mode)) {if (strcmp (".", entry -> d_name) == 0 | | strcmp ("..", entry -> d_name) == 0) {continue;} Addbytes (statbuf.st_size); Check (entry -> d_name);} Else{addbytes (statbuf.st_size);}} ChDir (".."); Closedir (DP);}}; Int main () {char topdir[100] = "/home/oracle/neeq/source/";//printf ("Directory Scan of dir: %s\n ", topdir); Checkspace cs = checkspace (TOPDIR); Cs. Display ();//printf ("done.\n"); return 0;}
The results of the program run as follows:
by calculator:
2*1024*1024*1024+933*1024*1024+847*1024+519=3126672903
This result is consistent with the results of DU statistics.
END
C + + programs under Linux: Count the total amount of space that a directory and its internal files occupy