Part 1. #include #include #include #include using namespace std; struct dir_info //structure

Part 1.

#include

#include

#include

#include

using namespace std;

struct dir_info //structure dir_info declaration

{

int dcount;

int fcount;

int fsize;

};

struct dir_info filecnt(char *); //function prototype of function filecnt(char *)

int main(int argc,char *argv[])

{

char path[1000];

struct dir_info temp;

cout<<"Enter path of the directory : ? ";

cin>>path; //read directory path

temp=filecnt(path); //get directory information by calling function filecnt(path)

//print the result obtained from function filecnt(path)

cout<<"The total number of directories in directory "<< path<<" is :"<<temp.dcount<<endl;

cout<<"The total number of files in directory "<< path<<" is :"<< temp.fcount<<endl;

cout<<"The total number of bytes occupied by all files in directory "<< path<<" is :"<<temp.fsize<<endl;

return 0;

}

//Function definition of void filecnt(char *Path)

//argument:directory path name

//return:dir_info

struct dir_info filecnt(char *Path)

{

struct dir_info temp1;

char newpath[1000]; //This array is used to create subdirectory path name

static int flag=0,dir_count=0,file_count=0,file_size=0;

DIR *parentDir;

struct dirent *dirent;

parentDir= opendir(Path); //open directory

if (parentDir == NULL && flag==0) //if parent directory can’t be open at first time,print error message

{

cout<< "Error opening directory"<<Path<<endl;

exit (-1);

}

while ((dirent= readdir(parentDir)) != NULL) //read all files and sub directories in the parent directory

{

flag=1;

if (strcmp((*dirent).d_name, “.”) != 0 && strcmp((*dirent).d_name, “..”) != 0) //if checking item is not current directory or parent dirctory

{

if((*dirent).d_type==DT_DIR) //if checking item is directory

{

dir_count++; //increment directory count

strcpy(newpath, Path); // Construct new path from our parent path

strcat(newpath, “/”);

strcat(newpath, (*dirent).d_name);

filecnt(newpath); //recursively call filecnt(newpath) function for getting informations in sub directory

}

if((*dirent).d_type==DT_REG) //if checking item is plain file

{

file_count++; //increment file count

file_size=file_size+(*dirent).d_reclen; //update total file size

}

}

}

closedir(parentDir); //close directory

temp1.dcount=dir_count;

temp1.fcount=file_count;

temp1.fsize=file_size;

return temp1; //return directory information

}