Extract String From Text 1.0

时间:2022-09-13 13:39:48

 main.cpp

 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <fstream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 string readLine ( const char* filepath ) {
 9     ifstream file;
10     file.open ( filepath );
11     string text;
12     if ( file.is_open() ) {
13         while ( !file.eof() ) {
14             getline ( file, text );
15             break;
16         }
17     }
18     file.close();
19     return text;
20 }
21 
22 int main ( int argc, char* argv[] ) {
23     string opt = argv[1];
24     if ( !opt.compare ( "-p" ) ) {
25         string text = readLine ( argv[3] );
26         size_t found = text.find ( argv[2] );
27         if ( found != string::npos ) {
28             cout << found << endl;
29             return 0;
30         }
31     } else if ( !opt.compare ( "-e" ) ) {
32         string text = readLine ( argv[4] );
33         cout << text.substr ( atoi ( argv[2] ), atoi ( argv[3] ) ) << endl;
34         return 0;
35     }
36     return -1;
37 }

Extract String From Text 1.0

附件1