Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions C/sizeofdirectory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This C program scan the current directory and display total size in bytes

Header file and function used

The type DIR is defined in the header file <dirent.h>

The readdir() function returns a pointer to a structure representing the directory entry at the current position in the stream
27 changes: 27 additions & 0 deletions C/sizeofdirectory/sizeoffolder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <dirent.h>

int main(void)
{
struct dirent *de; // Pointer for directory
int ctr = 0; // variable to store size
DIR *dr = opendir(".");

if (dr == NULL) //if directory not exist
{
printf("Can't open current directory" );
return 0;
}

while ((de = readdir(dr)) != NULL) //if directory exists and till the end of directory
{
FILE* fp = fopen(de->d_name, "r");
fseek(fp, 0L, SEEK_END);
ctr = ctr + ftell(fp); // calculating the size of each file in folder
fclose(fp);
}

printf("Directory Size : %d bytes", ctr);
closedir(dr);
return 0;
}