-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
89 lines (75 loc) · 2.75 KB
/
main.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>
#include <iomanip>
#include "file_size.cpp"
#include "file_lines.cpp"
#include "file_words.cpp"
#include "file_char.cpp"
using namespace std;
int main(int argc, char **argv) {
string options[] = {"-c","-l","-w","-m"};
if(argc == 3){
string ioption(argv[1]);
int ioptionc = -1;
for(int i=0;i<4;i++){
if(argv[1]==options[i]){
ioptionc = i;
break;
}
}
if(ioptionc == -1){
cout << "Error: Please enter a valid option\n";
cout << "-c: " << "To obtain file size\n";
cout << "-l: " << "To obtain number of lines in a file\n";
cout << "-w: " << "To obtain number of words in a file\n";
cout << "-m: " << "To obtain number of characters in a file\n";
return 1;
}
ifstream file(argv[2]);
if(file.fail()){
cout << "Error: File not found, please enter a valid file name\n";
return 1;
}
switch (ioptionc) {
case 0:
cout << "Size of the file " << argv[2] << " is: " << file_size(argv[2]) << "\n";
break;
case 1:
cout << "Number of lines in the file " << argv[2] << " is: " << file_lines(argv[2]) << "\n";
break;
case 2:
cout << "Number of words in the file " << argv[2] << " is: " << file_words(argv[2]) << "\n";
break;
case 3:
cout << "Number of characters in the file " << argv[2] << " is: " << file_char(argv[2]) << "\n";
break;
}
}else if(argc == 2){
string ifile_name(argv[1]);
if(ifile_name=="help"){
cout << "\nwc-cli helps you to obtain details about any file!\n";
cout << "\n";
cout << "[Options]\n";
cout << "-c\tTo obtain size of the file\n";
cout << "-l\tTo obtaing total number of lines in the file\n";
cout << "-w\tTo obtain total number of words in the file\n";
cout << "-m\tTo obtain total number of characters in the file\n";
cout << "\n";
cout << "Example command\n";
cout << "./wc -l <path_of_file>\n";
cout << "\n";
}else{
ifstream file(ifile_name);
if(file.fail()){
cout << "Error: File not found, please enter a valid file name\n";
return 1;
}
cout << "File: " << ifile_name << "\n";
cout << "Size: " << file_size(ifile_name) << " bytes\n";
cout << "Lines: " << file_lines(ifile_name) << "\n";
cout << "Words: " << file_words(ifile_name) << "\n";
cout << "Characters: " << file_char(ifile_name) << "\n";
}
}
return 0;
}