Advertisement
_br0uh_

Untitled

Sep 18th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. int LineCountingInFile(std::ifstream& fin);
  6. int SizeOfFile(std::ifstream& fin);
  7. int numOfWords(std::ifstream& fin);
  8.  
  9. int LineCountingInFile(std::string path){
  10.     std::ifstream fin(path);
  11.     char *line;
  12.     std::string file_name;
  13.     int line_counter = 0;
  14.         while(!fin.eof()){
  15.             fin.getline(line, 1000);
  16.             line_counter++;
  17.     }
  18.     return line_counter ;
  19. }
  20.  
  21.  
  22. int SizeOfFile(std::string path){
  23.     std::ifstream fin(path, std::ios::binary);
  24.     std::string file_name;
  25.     fin.seekg(0, ios_base::end)
  26.     int size = fin.tellg();
  27.     fin.close();
  28.     return size;
  29. }
  30.  
  31.  
  32. int numOfWords(std::string path){
  33.     std::ifstream fin(path);
  34.     std::string fileName, words, str;
  35.     int numCount =0;
  36.  
  37.     while(fin >> words){
  38.         ++numCount;
  39.     }
  40.     fin.close();
  41.     return numCount;
  42.  
  43. }
  44.  
  45. int main( int argc, char* argv[]) {
  46.     std::string name_of_file;
  47.     bool check_lines = false;
  48.     bool check_bytes = false;
  49.     bool check_words = false;
  50.     for (int i = 0; i < argc; ++i) {
  51.         std::string word_from_cmd = argv[i];
  52.         int n = word_from_cmd.size();
  53.         if (word_from_cmd[0] == '-') {
  54.             if (word_from_cmd[1] == '-') {
  55.                 if (word_from_cmd[3] == 'l') {
  56.                     check_lines = true;
  57.                 }
  58.                 if(word_from_cmd[3] == 'b') {
  59.                     check_bytes = true;
  60.                 }
  61.                 if (word_from_cmd[3] == 'w') {
  62.                     check_words = true;
  63.                 }
  64.             }
  65.             else{
  66.                 for(int j = 0; j < n; ++j) {
  67.                     if (word_from_cmd[j] == 'l') {
  68.                         check_lines = true;
  69.                     }
  70.                     if (word_from_cmd[j] == 'b') {
  71.                         check_bytes = true;
  72.                     }
  73.                     if (word_from_cmd[j] == 'w') {
  74.                         check_words = true;
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     std::string file_name;
  81.     for (int i = 1; i < argc; i++) {
  82.         std::string argument = argv[i];
  83.         for(int j = 0; j < argument.size(); j++){
  84.             if (argument[j] == '.'){
  85.                 file_name = argument;
  86.             }
  87.         }
  88.  
  89.     }
  90.     if (check_bytes) {
  91.    std::cout<< SizeOfFile(file_name);
  92.     }
  93.  
  94.     if (check_words) {
  95.         std::cout<< numOfWords(file_name);
  96.  
  97.     }
  98.  
  99.     if (check_lines){
  100.         std::cout<< LineCountingInFile(file_name);
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement