Skip to content

Code #1

@notebookdata

Description

@notebookdata

#include <boost/filesystem.hpp>
//#include
#include
#include <unistd.h>
#include

using namespace std;
using namespace boost::filesystem;
unsigned int microseconds = 100000;
bool showCurStr = false;

void listDir(const path &p, string search){
if(p.empty())
return;
if(is_directory(p)){
std::vector<directory_entry> v;
copy(directory_iterator(p), directory_iterator(), back_inserter(v));
//std::cout << p << " is a directory containing:\n";

    for ( std::vector<directory_entry>::const_iterator it = v.begin(); it != v.end();  ++ it )
    {
        listDir(it->path(), search);
        //usleep(microseconds);
        //std::cout<< (*it).path().string()<<endl;
    } 
}
else {
    std::ifstream file;
    file.open(p.c_str());
    string line;
    //string search = "Hello";
    unsigned int curLine = 0;
    for(; getline(file, line); curLine++) {
        if (line.find(search) != string::npos) {
            cout << "found: " << search << " " << p << " line: " << curLine << endl;
            if(showCurStr)
                cout << '"' << line << '"' << endl;
        }
    }
    //std::cout << p << endl;
    file.close();
}

}

int main(int argc, char *argv[]){

path p(argc>1? argv[1] : ".");
string searchString;
if(argc > 2){
    searchString = argv[2];
    cout << "Searching " << searchString << endl;
}
else
{
    cout << "what to search?" << endl;
    return 0;
}
if(argc > 3)
    showCurStr = true;


//string searchString(argc>2? argv[2]:return 0);
// std::vector<directory_entry> v; // To save the file names in a vector.

// if(is_directory(p))
// {
//     copy(directory_iterator(p), directory_iterator(), back_inserter(v));
//     std::cout << p << " is a directory containing:\n";

//     for ( std::vector<directory_entry>::const_iterator it = v.begin(); it != v.end();  ++ it )
//     {
//         std::cout<< (*it).path().string()<<endl;
//     }    
// }
listDir(p, searchString);

return 0;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions