-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplaceString.cpp
60 lines (48 loc) · 1.17 KB
/
ReplaceString.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
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
#include <iostream>
#include <fstream>
#include<cstring>
using namespace std;
int main(int argc, char *argv[]) {
if (argc < 4) {
cout << "\nInsufficient number of arguments.";
return 1;
}
string input, output;
fstream fin;
fin.open(argv[1], ios::in);
cout << "\nFind String " << argv[2] << " and replace with " << argv[3];
if (!fin) {
cout << "\nCan't open the file.";
return 1;
}
while (fin) {
getline(fin, input);
cout << input;
cout << endl;
}
fin.close();
fin.open(argv[1], ios::in);
output = "";
if (!fin) {
cout << "\nCan't open the file.";
return 1;
}
while (fin) {
getline(fin, input);
int a = (int) input.find(argv[2], 0);
while (a != string::npos) {
input.replace(a, strlen(argv[2]), argv[3]);
a = (int) input.find(argv[2], (a + 1));
}
output = output + input + "\n";
}
fin.close();
fin.open(argv[1], ios::out);
if (!fin) {
cout << "\nCan't open the file.";
return 1;
}
fin << output;
cout << "\nData replaced.";
fin.close();
}