How to search multiple files

This is a personal project, not related to school or work.

I'm on windows 7, running command line.

The program i'm writing should work simular to the dos find command, only output line numbers also.
example:
find "123" "out*.txt"


If the only file in the folder I have is out.txt the code below will search for the matching string.
However if there is also a out1.txt, the code below displays the help-usage msg.

What I'm asking is :
When I use a wild card from the command line, what do I need to add to my code and have it search multiple files, such as:
command: searchfileforstring "123" "out*.*"
files: out.txt, out1.txt, out.old, out1.old.

Ps. I found I have to use quotes on long file names or it sees the space as another argv.

I found a reference to "regex" but have not found a good working example yet. Still looking.

out.txt file example:

123
234
345


*** This works but only if there is only 1 matching file name.
output example:
C:\Temp>searchfileforstring "123" "out*.*"
Searching out.txt for "123"
Line# File Content
--------------------------------------------------
1: 123


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Search file for String */
/* output line numbers, matching line*/

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int offset;
int counter;
string line;
ifstream InputFile;

void Directions ()
{
        cout << "Usage is <Prompt> SearchFileForString " << " " << '"'<< "Search String" << '"' << " " << '"' << "DriveLetter:\\path\\filename" << '"' << endl;
        cout << "NOTE: The string and path should be in quotes to prevent errors."  << endl;
}

int main(int argc, char* argv[])
{
int LineCount=0;
// Check the value of argc. If not enough parameters have been passed, inform user and exit.
    if (argc != 3)               // Inform the user of how to use the program
    {
    char *s0 = argv[0];          // Command
    char *s1 = argv[1];          // Search String
    char *s2 = argv[2];          // File to Search
    Directions ();               // Path or file name with spaces must be inside quotes.
    exit(0);
    } 

    // if we have enough parameters...
    else {
       InputFile.open (argv[2]);
          if(InputFile.is_open())
          {
          cout << "Searching " << argv[2] << " for " << '"' << argv[1] << '"' << endl;
  cout << "Line#" << "\t" << "File Content" << endl;
  cout << "--------------------------------------------------" << endl;

          while(!InputFile.eof())
          {
          getline(InputFile,line);
          LineCount++;
          
          if ((offset = line.find(argv[1], 0)) != string::npos)
          {
          // If match found, Do this
          cout << LineCount << ": ";
          cout << "\t" << line << endl;
//          cout << "String " << "'" << argv[1] << "'" << " found " << argv[2] << endl;
//          break;          
          } // End if
          } // End while
          } // End if
          else
          {
          cout << "Can not open file, " << "'"<< argv[2] << "'" << " Check Path and filename." << endl << endl;
          Directions ();
          }
    InputFile.close();         
    }
    return 0;
}

Last edited on
Topic archived. No new replies allowed.