Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int LineCountingInFile(std::ifstream& fin);
- int SizeOfFile(std::ifstream& fin);
- int numOfWords(std::ifstream& fin);
- int LineCountingInFile(std::string path){
- std::ifstream fin(path);
- char *line;
- std::string file_name;
- int line_counter = 0;
- while(!fin.eof()){
- fin.getline(line, 1000);
- line_counter++;
- }
- return line_counter ;
- }
- int SizeOfFile(std::string path){
- std::ifstream fin(path, std::ios::binary);
- std::string file_name;
- fin.seekg(0, ios_base::end)
- int size = fin.tellg();
- fin.close();
- return size;
- }
- int numOfWords(std::string path){
- std::ifstream fin(path);
- std::string fileName, words, str;
- int numCount =0;
- while(fin >> words){
- ++numCount;
- }
- fin.close();
- return numCount;
- }
- int main( int argc, char* argv[]) {
- std::string name_of_file;
- bool check_lines = false;
- bool check_bytes = false;
- bool check_words = false;
- for (int i = 0; i < argc; ++i) {
- std::string word_from_cmd = argv[i];
- int n = word_from_cmd.size();
- if (word_from_cmd[0] == '-') {
- if (word_from_cmd[1] == '-') {
- if (word_from_cmd[3] == 'l') {
- check_lines = true;
- }
- if(word_from_cmd[3] == 'b') {
- check_bytes = true;
- }
- if (word_from_cmd[3] == 'w') {
- check_words = true;
- }
- }
- else{
- for(int j = 0; j < n; ++j) {
- if (word_from_cmd[j] == 'l') {
- check_lines = true;
- }
- if (word_from_cmd[j] == 'b') {
- check_bytes = true;
- }
- if (word_from_cmd[j] == 'w') {
- check_words = true;
- }
- }
- }
- }
- }
- std::string file_name;
- for (int i = 1; i < argc; i++) {
- std::string argument = argv[i];
- for(int j = 0; j < argument.size(); j++){
- if (argument[j] == '.'){
- file_name = argument;
- }
- }
- }
- if (check_bytes) {
- std::cout<< SizeOfFile(file_name);
- }
- if (check_words) {
- std::cout<< numOfWords(file_name);
- }
- if (check_lines){
- std::cout<< LineCountingInFile(file_name);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement