-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.cpp
More file actions
72 lines (61 loc) · 1.67 KB
/
main2.cpp
File metadata and controls
72 lines (61 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <dirent.h>
#include <sys/stat.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
//adding comment
//adding second comment
void explore(char *dir_name){
DIR *dir; //pointer to an open directory
struct dirent *entry; //stuff in the directory
struct stat info; //information about each entry
//1 open
dir = opendir(dir_name);
if(!dir){
cout << "Directory was not found\n" << endl;
return;
}
int file_count = 0;
int folder_count = 0;
int total_file_size = 0;
//2 read
while( (entry = readdir(dir)) != NULL){
if( entry->d_name[0] != '.'){
string path = string(dir_name) + "/" + string(entry->d_name);
cout << "Entry = " << path << endl;
stat(path.c_str(),&info); //
if(S_ISDIR(info.st_mode)){
folder_count++;
explore((char*)path.c_str());
}
else{
file_count++;
stat(dir_name, &info);
total_file_size += info.st_size;
}
}
}
cout << "Directory " << dir_name << " has " << file_count << " files" << endl;
cout << "Directory: " << dir_name << " has " << folder_count << " directories" << endl;
cout << "total file size is " << total_file_size << " bytes" << endl;
//3 close
closedir(dir);
}
static int version = 10;
void displayVersion(){
cout << "Version: " << version << ".\n " << endl;
}
int main(int argc, char ** argv)
{
displayVersion();
//should this be the argument we accept?
if(argc == 2)
explore((char*)argv[1]);
else
{
explore((char*)".");
}
return 0;
}