File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ This C program scan the current directory and display total size in bytes
2
+
3
+ Header file and function used
4
+
5
+ The type DIR is defined in the header file <dirent.h>
6
+
7
+ The readdir() function returns a pointer to a structure representing the directory entry at the current position in the stream
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <dirent.h>
3
+
4
+ int main (void )
5
+ {
6
+ struct dirent * de ; // Pointer for directory
7
+ int ctr = 0 ; // variable to store size
8
+ DIR * dr = opendir ("." );
9
+
10
+ if (dr == NULL ) //if directory not exist
11
+ {
12
+ printf ("Can't open current directory" );
13
+ return 0 ;
14
+ }
15
+
16
+ while ((de = readdir (dr )) != NULL ) //if directory exists and till the end of directory
17
+ {
18
+ FILE * fp = fopen (de -> d_name , "r" );
19
+ fseek (fp , 0L , SEEK_END );
20
+ ctr = ctr + ftell (fp ); // calculating the size of each file in folder
21
+ fclose (fp );
22
+ }
23
+
24
+ printf ("Directory Size : %d bytes" , ctr );
25
+ closedir (dr );
26
+ return 0 ;
27
+ }
You can’t perform that action at this time.
0 commit comments