Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <dirent.h> // directory header
  2. #include <iostream>
  3. #include <string.h>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. bool txtCheck(char const *name){
  10. size_t len = strlen(name);
  11. return len > 4 && strcmp(name + len - 4, ".txt") == 0;
  12. }
  13. int main(int argc, char** argv) {
  14. string word = " ";
  15.  
  16. for (int a = 1; a <argc; a++){
  17.  
  18. DIR *current_dir; //Creates a pointer to point to the current directory
  19. current_dir = opendir("."); //Open the directory at "." - apperently this means the current folder
  20. dirent *current_fil = NULL; //Create the file pointer, this will have the info of the files
  21.  
  22. int lineCounter = 1;
  23. int dupeCounter = 0;
  24. bool dupe = false;
  25.  
  26. cout << argv[a] << ": " << endl;
  27. while(current_fil = readdir(current_dir)){ //Read the info for one file, then send it into the current file pointer
  28.  
  29. ifstream file (current_fil->d_name);
  30.  
  31. dupeCounter = 0;
  32. string fileName = " ";
  33. char letter = ' ';
  34. fileName = current_fil->d_name;
  35.  
  36. if (txtCheck(current_fil->d_name) == 1){
  37. lineCounter = 1;
  38. while(!file.eof()){
  39. file >> word;
  40.  
  41. while(file.peek() == 13 || file.peek() == 10){
  42. if (file.peek() == 13 || file.peek() == 10){
  43. file.get();
  44. if (file.peek() == 13 || file.peek() == 10){
  45. lineCounter ++;
  46. }
  47. }
  48. }
  49. dupe = false;
  50. if (word == argv[a]){
  51. dupeCounter ++;
  52. dupe = true;
  53. if (dupeCounter == 1 || dupe == true){
  54. cout << "Found in file " << current_fil->d_name << " at line: " << lineCounter << endl;
  55. } if (dupeCounter > 1){
  56. cout << "Found in file " << current_fil->d_name << " " << dupeCounter << " times" << endl;
  57. } else if (dupeCounter == 0){
  58. cout << "Not found " << endl;
  59. }
  60. }
  61.  
  62. }
  63. }
  64. file.close();
  65. }
  66. closedir(current_dir); //close the stream
  67. }
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement