Link to home
Start Free TrialLog in
Avatar of Neil Shah
Neil Shah

asked on

Write a C program to calculate the total size of the files in a directory and the sub-directories of the directory. 

Write a C program to calculate the total size of the files in a directory and the sub-directories of the directory. 
Avatar of arnold
arnold
Flag of United States of America image

Ok.

What have you come up with so far?

It should be straight forward, have you worked out the logic involved?

If you were personally tasked with counting contents of a large box that might include other boxes, etc., what is the process?
Avatar of phoffric
phoffric

https://www.experts-exchange.com/questions/21046070/Recursive-processing-of-subdirectories.html?anchorAnswerId=11457702#a11457702
 this post should help you get you started. It has some key functions that are necessary for you to traverse the directory tree structure. Code may not be exactly what you want, but some functions such as dirent and readdir may prove useful to you .
while ((dirent = readdir(dir)) != NULL)      /* until read hits EOF */
 {
   if (errno == EBADF)                             
    /* if NULL happens because of error, not EOF */
     syserr("readdir");                                /* report error and exit */
   if (dirent->d_name[0] != '.')                 /* don't store as files anything staring with '.' */
    {    
      file_path = create_path(dirent->d_name);  /* create full pathname for file */
      get_file_info(file_path, dirent->d_name, statbuf, passwd);
                                                                /* store the file info */       
       insert_ok = insert_ordered(lp, file);  /* insert files onto list sorted by filename */  
    }         /* end if */     
  } 

Open in new window

Avatar of Neil Shah

ASKER

Here is what I have so far:
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include<sys/syscall.h>
size_t getFilesize(const char* filename){
    struct stat st;
    return st.st_size;
}
int main(int argc , char *argv[]){
    char* directory = argv[1];
    struct dirent *de;
    
   
    DIR *dr = opendir(directory);
    
    if(dr == NULL) printf("Could not open current directory"); 
    
    while(de = readdir(dr)){
        printf("%s\t%lu\n", de->d_name, getFilesize(de));
    }
    
    closedir(dr);
    
    return 0;
}

Open in new window


This is the error that I get:
User generated image
the last block of error is the recent one
Do you need only the total size of the to
I.e. Calculate thisdirectory
thisdirectory 456789654

Your current approach would yield a single pass Once the errors of type mismatch are corrected.

You shoukd either use iterative or recursive function to which a directory is passed, and the data from file sizes are summed into ...

I.e you should also test to make sure you are given a directory and not a file.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.