Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <filesystem>
- int optionsChecker(std::string opt){
- if (opt == "-l" or opt == "--lines"){
- return 1;
- } else if (opt == "-c" or opt == "-bytes"){
- return 2;
- } else if (opt == "-w" or opt == "--words"){
- return 3;
- } else if (opt == "-m" or opt == "--chars"){
- return 4;
- } else {
- std::cout << "There's no " << opt << " option." << std::endl;
- return 5;
- }
- }
- int main(int argc, char** argv){
- bool options[6] = {false, false, false, false, false, false};
- /*
- * 0 - Хотя бы одна опция задана
- * 1 - -l, --lines вывод количества строк
- * 2 - -c, --bytes вывод размера файла в байтах
- * 3 - -w, --words вывод количества слов
- * 4 - -m, –chars вывод количества букв
- * 5 - error
- */
- double counters[4];
- /*
- * 0 - кол-во строк
- * 1 - размер файла
- * 2 - кол-во слов
- * 3 - кол-во букв
- */
- std::string pathFile;
- std::string strFile;
- for (int i = 1; i < argc; i++){
- if (argv[i][0] == '-'){
- options[optionsChecker(argv[i])] = true;
- options[0] = true;
- } else {
- for (int j = 0; j < 4; j++){
- counters[j] = 0;
- }
- pathFile = argv[i];
- std::ifstream file(pathFile);
- if (!file.is_open()){
- std::cout << "File can't be opened." << std::endl;
- } else {
- while (std::getline(file, strFile)) {
- counters[0] += 1;
- for (int t = 0; t < strFile.size(); t++) {
- if (int(strFile[t]) == 32 or int(strFile[t]) == 127) {
- counters[2] += 1;
- } else if (int(strFile[t]) > 32 or int(strFile[t]) < 127) {
- counters[3] += 1;
- }
- }
- counters[2] += 1;
- }
- //counters[2] += 1;
- std::filesystem::path p{argv[i]};
- counters[1] = std::filesystem::file_size(p);
- file.close();
- if (!options[0]) {
- for (int t = 0; t < 4; t++) {
- std::cout << counters[t] << " ";
- }
- std::cout << argv[i] << std::endl;
- } else {
- for (int t = 1; t < 5; t++) {
- if (options[t]) {
- std::cout << counters[t - 1] << " ";
- }
- }
- std::cout << argv[i] << std::endl;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement