0

I have to write a program that will go through a given folder and use regex_search to find every instance of a certain string. I've got the regex_search working on it's own now, and I'm just trying to figure out how to go through each file. I want to attempt it using directory but am unsure where I would put it. Would I have to put the search through the file into my main method or would I have to create a seperate function outside of the main method for going through each file and call on it within the main method?

This is what I have now. Any tips you guys can give on how to approach this would be greatly appreciated!

Right now the function of it is to read an input text file and output a txt file that shows all the instances and the line number of each apperance. I am not required to see which lines they are on, use a particular file, or make an output file for this program, what I find will simply be printed to the console. I've left what I have now because I'm not sure if I'll checking each indivdual file in a similar fashion just with a different cariable name.

#include <iostream>
#include <regex>
#include <string>
#include <fstream>
#include <vector>
#include <regex>
#include <iomanip>

using namespace std;

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

// validate the command line info
if( argc < 2 ) {
    cout << "Error: Incorrect number of command line arguments\n"
            "Usage: grep\n";
    return EXIT_FAILURE;
}

//Declare the arguments of the array
    string resultSwitch = argv[1]; 
string stringToGrep = argv[2];
string folderName = argv [3];
regex reg(stringToGrep);


// Validate that the file is there and open it
ifstream infile( inputFileName );
if( !infile ) {
    cout << "Error: failed to open <" << inputFileName << ">\n"
            "Check filename, path, or it doesn't exist.\n";
    return EXIT_FAILURE;
}



while(getline(infile,currentLine))
{
    lines.push_back( currentLine ); 
            currentLineNum++;
            if( regex_search( currentLine, reg ) )
                    outFile << "Line " << currentLineNum << ": " << currentLine << endl;



}

    infile.close();
}
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Sh0gun
  • 871
  • 4
  • 10
  • 16
  • Why `lines` vector? You're not using it. – m0skit0 Apr 16 '12 at 22:02
  • Yes, I've gotten rid o that and changed a few things, switches is there because at some point I need switches. They will just be my last concern. – Sh0gun Apr 16 '12 at 22:05
  • you asking about structure of program? if you want flexible code, you must separate every logical structure/step. so, `readFolder`, `readFile`, `SearchInFile` is the best practice. also, if you know about a classes, write OO-designed code – gaussblurinc Apr 16 '12 at 22:23
  • Use glob() to skip regex library http://stackoverflow.com/questions/8401777/simple-glob-in-c-on-unix-system – normalUser Jul 07 '16 at 12:15

1 Answers1

3

Reading a directory/folder is operating system dependent. In a UNIX/Linux/MacOS world, you use opendir(), and readdir():

#include <sys/types.h>
#include <dirent.h>

...

DIR *directory = opendir( directoryName );

if( directory == NULL )
    {
    perror( directoryName );
    exit( -2 );
    }
// Read the directory, and pull in every file that doesn't start with '.'

struct dirent *entry;
while( NULL != ( entry = readdir(directory) ) )
{
// by convention, UNIX files beginning with '.' are invisible.
// and . and .. are special anyway.
    if( entry->d_name[0] != '.'  )
        {
        // you now have a filename in entry->d_name;
        // do something with it.
        }
}
DRVic
  • 2,481
  • 1
  • 15
  • 22