in Education by
I am trying to write a basic program that will sum up the filesizes of all files in the current directory and all subdirectories. I thought it would be a nice exercise. I am trying to use basic recursion for this. Here is what I have so far: #include #include #include #include #include long totalbytes = 0; long dirsize(const char* directory, int verbose) { struct dirent *de; struct stat s; DIR * dir; //long total_items = 0; long filesize = 0; dir = opendir(directory); if (dir == NULL) { printf("Failed to open %s.\n", directory); return -1; } while ((de = readdir (dir)) != NULL) { stat(de->d_name, &s); if (S_ISLNK(s.st_mode)) { printf("links are ignored.\n"); } if (de->d_type == DT_REG) { filesize = 0; //be sure to reset this each time to avoid inaccuracy stat(de->d_name, &s); // get file info into our s structure if (verbose) { printf("%s/%s : %ld bytes (%f MB)\n", directory, de->d_name, s.st_size, (float) s.st_size / 1024 / 1024); } filesize = s.st_size; //put file size into filesize variable totalbytes += filesize; //increment totalbytes } if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) { char pathname[PATH_MAX]; sprintf(pathname, "%s/%s", directory, de->d_name); dirsize(pathname, verbose); //recursion: keep looping until no more subdirs remain } } closedir(dir); return totalbytes; } long compute_size(const char* directory, int verbose) { long space = dirsize(directory, verbose); return space; } int main(int argc, char* argv[]) { if (argc != 2) { printf("Usage: dirsize DIRECTORY\n"); return -1; } int verbose = 1; //show or hide individual computations long space = compute_size(argv[1], verbose); if (space != -1) { float space_mb = (float) space / 1024 / 1024; printf("space occupied: %ld bytes\n", space); printf("(%f MB)\n", space_mb); } return 0; } Here is my program output (I am also including an ls of the subdirectory) 08:35 PM@~/tmp$ ./dirsize . ./dirsize : 8369 bytes (0.007981 MB) ./diskstat.c : 1430 bytes (0.001364 MB) ./diskstat : 7993 bytes (0.007623 MB) ./ftw.c : 491 bytes (0.000468 MB) ./a.out : 8044 bytes (0.007671 MB) ./arrays.sh : 212 bytes (0.000202 MB) ./fileread/timestamp : 0 bytes (0.000000 MB) ./fileread/timestamp.c : 0 bytes (0.000000 MB) ./fileread/a.out : 8044 bytes (0.007671 MB) ./fileread/build.prop : 8044 bytes (0.007671 MB) ./fileread/read.c : 4096 bytes (0.003906 MB) ./fileread/read : 454656 bytes (0.433594 MB) ./local.fstab : 76 bytes (0.000072 MB) ./dirsize.c : 1857 bytes (0.001771 MB) ./echo.sh : 223 bytes (0.000213 MB) ./TEAMS.sh : 605 bytes (0.000577 MB) space occupied: 504140 bytes (0.480785 MB) 08:35 PM@~/tmp$ ls -l fileread/ total 40 -rwxr-xr-x 1 raidzero raidzero 8132 Dec 28 10:39 a.out -rw-r--r-- 1 raidzero raidzero 2346 Dec 28 10:09 build.prop -rwxr-xr-x 1 raidzero raidzero 8384 Dec 29 13:57 read -rw-r--r-- 1 raidzero raidzero 1150 Dec 28 10:16 read.c -rwxr-xr-x 1 raidzero raidzero 8132 Dec 29 13:57 timestamp -rw-r--r-- 1 raidzero raidzero 659 Dec 28 10:39 timestamp.c 08:35 PM@~/tmp$ In my limited experience, getting random data is sign of improper memory allocation, but st_size in the stat struct is a long int..? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
de->d_name only has the filename - it does not contain the path leading up to it. So you're statting ./timestamp rather than ./fileread/timestamp, for example. You can either construct the full path to the file, use chdir() to enter directories as you go (not safe if you're in a multi-threaded environment, and can be fragile if dirs are moved when you're in them - fchdir() can be very helpful to get back to higher directories), or (on recent unixen) use the openat()/fstat() functions to traverse directories. Note that, had you checked for error returns from stat, you would have noticed this. Always check those error returns.

Related questions

0 votes
0 votes
    DROP TYPE Position; CREATE OR REPLACE TYPE Position AS OBJECT (longitude NUMBER(11,7), lattitude NUMBER(11, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    For ____________, the error is determined by getting the proportion of values misclassified by the model. Classification Clustering None of the options Regression...
asked Aug 28, 2021 in Technology by JackTerrance
0 votes
    For ____________, the error is determined by getting the proportion of values misclassified by the model. (1)Regression (2)Clustering (3)Classification (4)None of the options...
asked May 22, 2021 in Technology by JackTerrance
0 votes
    I want to implement in Java a class for handling graph data structures. I have a Node class and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    What is the default clone of HashSet? (a) Deep clone (b) Shallow clone (c) Plain clone (d) Hollow ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    I have a Visual Studio online Team Project with a repository. I have been using this with VS 2013 for a ... and doing clone again? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    (1) write the uses of colour pallet in Corel draw software?? (2) write the uses of clone command?? tomorrow ... guys of this question . Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    Which of these method of Object class can clone an object? (a) Objectcopy() (b) copy() (c) ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    What is the default clone of HashSet? (a) Deep clone (b) Shallow clone (c) Plain clone (d) ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What is the difference between Hibernate and EJB 3? Do not you think EJB 3 is just a clone of Hibernate?...
asked Jun 5, 2021 in Technology by JackTerrance
0 votes
    How can I clone or deep copy an object so that the cloned object can be modified without any changes being reflected in the original object?...
asked Jan 15, 2021 in Technology by JackTerrance
0 votes
    How can we clone a Git repository into a specific folder?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    How to clone all remote branches in Git?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    Can we create a clone of a singleton object?...
asked Nov 7, 2020 in Education by Editorial Staff
...