Skip to content

Commit aefcc3a

Browse files
authored
Merge pull request #289 from japneetbhatia/main
Changes done (size of folder)
2 parents 355451e + 793b90e commit aefcc3a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Diff for: C/sizeofdirectory/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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

Diff for: C/sizeofdirectory/sizeoffolder.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)