hi, my search in directory code:
[cpp]#include <iostream>
#include <fstream>
#include <errno.h>
#include <dirent.h>
#include <conio.h>
#include <vector>
#include <map>
typedef std::vector<std::string> Files;
typedef std::map<std::string, int> Times;
bool getFiles(const std::string& dir, Files& files)
{
DIR *d = opendir(dir.c_str());
struct dirent* p;
if(d == NULL) {
std::cout << "Unable to open directory" << std::endl;
switch(errno) {
case EBADF:
std::cout << "Invalid directory stream descriptor.\n";
break;
case EACCES:
std::cout << "Permission denied.\n";
break;
case EMFILE:
std::cout << "Too many file descriptors in use by process.\n";
break;
case ENFILE:
std::cout << "Too many files are currently open in the system.\n";
break;
case ENOENT:
std::cout << "Entered path name does not exist, or is an empty string.\n";
break;
case ENOMEM:
std::cout << "Insufficient memory to complete the operation.\n";
break;
case ENOTDIR:
std::cout << "Entered path is not a directory.\n";
break;
default:
std::cout << "Unexpected error, code: " << errno << "\n";
break;
}
return false;
}
while(p = readdir(d))
files.push_back(std::string(p->d_name));
closedir(d);
return true;
}
int main()
{
Files files;
Times found;
std::string dir, search, lines;
std::cout << "Directory: ";
std::cin >> dir;
if(!getFiles(dir, files)) {
std::cout << "Failure!" << std::endl;
system("pause");
return 1;
}
char buffer;
int i = 0;
do {
i++;
if(i >= 2)
std::cout << std::endl;
std::cout << "What to search for: ";
std::cin >> search;
for(Files::iterator it = files.begin(); it != files.end(); ++it) {
std::string tmp = dir + "/" + *it;
std::ifstream file(tmp.c_str());
if(file.is_open()) {
while(!file.eof()) {
getline(file, lines);
if(lines.find(search) != std::string::npos) {
found[*it]++;
if(found[*it] > 1)
std::cout << "Found at " << *it << " " << found[*it] << " times, ";
else
std::cout << "Found at " << *it << " 1 time, ";
std::string a = (*it).substr((*it).rfind(".")), temp = "";
if(a == ".c")
temp = "C Source File.";
else if(a == ".cpp")
temp = "C++ Source File.";
else if(a == ".h")
temp = "Header file.";
else if(a == ".php")
temp = "PHP File.";
else if(a == ".lua")
temp = "LUA Source File.";
else if(a == ".py")
temp = "Python Source file.";
else if(a == ".java")
temp = "Java.";
else
temp = "Unknown.";
std::cout << "Type: " << temp << std::endl;
}
}
}
}
std::cout << "Continue: (y/N)?";
buffer = getch();
if(buffer != 121 && buffer != 89)
break;
} while(true);
std::cout << std::endl;
system("pause");
return 0;
}
[/cpp]
windows only, since linux uses a command.
[cpp]#include <iostream>
#include <fstream>
#include <errno.h>
#include <dirent.h>
#include <conio.h>
#include <vector>
#include <map>
typedef std::vector<std::string> Files;
typedef std::map<std::string, int> Times;
bool getFiles(const std::string& dir, Files& files)
{
DIR *d = opendir(dir.c_str());
struct dirent* p;
if(d == NULL) {
std::cout << "Unable to open directory" << std::endl;
switch(errno) {
case EBADF:
std::cout << "Invalid directory stream descriptor.\n";
break;
case EACCES:
std::cout << "Permission denied.\n";
break;
case EMFILE:
std::cout << "Too many file descriptors in use by process.\n";
break;
case ENFILE:
std::cout << "Too many files are currently open in the system.\n";
break;
case ENOENT:
std::cout << "Entered path name does not exist, or is an empty string.\n";
break;
case ENOMEM:
std::cout << "Insufficient memory to complete the operation.\n";
break;
case ENOTDIR:
std::cout << "Entered path is not a directory.\n";
break;
default:
std::cout << "Unexpected error, code: " << errno << "\n";
break;
}
return false;
}
while(p = readdir(d))
files.push_back(std::string(p->d_name));
closedir(d);
return true;
}
int main()
{
Files files;
Times found;
std::string dir, search, lines;
std::cout << "Directory: ";
std::cin >> dir;
if(!getFiles(dir, files)) {
std::cout << "Failure!" << std::endl;
system("pause");
return 1;
}
char buffer;
int i = 0;
do {
i++;
if(i >= 2)
std::cout << std::endl;
std::cout << "What to search for: ";
std::cin >> search;
for(Files::iterator it = files.begin(); it != files.end(); ++it) {
std::string tmp = dir + "/" + *it;
std::ifstream file(tmp.c_str());
if(file.is_open()) {
while(!file.eof()) {
getline(file, lines);
if(lines.find(search) != std::string::npos) {
found[*it]++;
if(found[*it] > 1)
std::cout << "Found at " << *it << " " << found[*it] << " times, ";
else
std::cout << "Found at " << *it << " 1 time, ";
std::string a = (*it).substr((*it).rfind(".")), temp = "";
if(a == ".c")
temp = "C Source File.";
else if(a == ".cpp")
temp = "C++ Source File.";
else if(a == ".h")
temp = "Header file.";
else if(a == ".php")
temp = "PHP File.";
else if(a == ".lua")
temp = "LUA Source File.";
else if(a == ".py")
temp = "Python Source file.";
else if(a == ".java")
temp = "Java.";
else
temp = "Unknown.";
std::cout << "Type: " << temp << std::endl;
}
}
}
}
std::cout << "Continue: (y/N)?";
buffer = getch();
if(buffer != 121 && buffer != 89)
break;
} while(true);
std::cout << std::endl;
system("pause");
return 0;
}
[/cpp]
windows only, since linux uses a command.
Last edited: