-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexec23-8.cpp
35 lines (30 loc) · 837 Bytes
/
exec23-8.cpp
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
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
using namespace std;
// Take as inputs a pattern and a file name, output numbered lines that contain a match of the pattern.
int main(int argc, char* argv[]) {
if (argc < 3) {
cout << "Usage: " << argv[0] << " pattern filename\n";
return 0;
}
regex pattern;
try {
pattern = argv[1];
}
catch (regex_error& e) {
cerr << "invalid regular expression: " << argv[1] << '\n';
return 1;
}
ifstream ifs(argv[2]);
if (!ifs) {
cerr << "can't open input file " << argv[2] << endl;
return 1;
}
string line;
for (int lineno = 1; getline(ifs, line); ++lineno)
if (regex_search(line, pattern))
cout << lineno << ": " << line << '\n';
return 0;
}