Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dirent.h> // directory header
- #include <iostream>
- #include <string.h>
- #include <fstream>
- #include <string>
- using namespace std;
- bool txtCheck(char const *name){
- size_t len = strlen(name);
- return len > 4 && strcmp(name + len - 4, ".txt") == 0;
- }
- int main(int argc, char** argv) {
- string word = " ";
- for (int a = 1; a <argc; a++){
- DIR *current_dir; //Creates a pointer to point to the current directory
- current_dir = opendir("."); //Open the directory at "." - apperently this means the current folder
- dirent *current_fil = NULL; //Create the file pointer, this will have the info of the files
- int lineCounter = 1;
- int dupeCounter = 0;
- bool dupe = false;
- cout << argv[a] << ": " << endl;
- while(current_fil = readdir(current_dir)){ //Read the info for one file, then send it into the current file pointer
- ifstream file (current_fil->d_name);
- dupeCounter = 0;
- string fileName = " ";
- char letter = ' ';
- fileName = current_fil->d_name;
- if (txtCheck(current_fil->d_name) == 1){
- lineCounter = 1;
- while(!file.eof()){
- file >> word;
- while(file.peek() == 13 || file.peek() == 10){
- if (file.peek() == 13 || file.peek() == 10){
- file.get();
- if (file.peek() == 13 || file.peek() == 10){
- lineCounter ++;
- }
- }
- }
- dupe = false;
- if (word == argv[a]){
- dupeCounter ++;
- dupe = true;
- if (dupeCounter == 1 || dupe == true){
- cout << "Found in file " << current_fil->d_name << " at line: " << lineCounter << endl;
- } if (dupeCounter > 1){
- cout << "Found in file " << current_fil->d_name << " " << dupeCounter << " times" << endl;
- } else if (dupeCounter == 0){
- cout << "Not found " << endl;
- }
- }
- }
- }
- file.close();
- }
- closedir(current_dir); //close the stream
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement