-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileReader.cpp
63 lines (47 loc) · 1.38 KB
/
fileReader.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
61
62
63
#include<iostream>
#include<cstdio>
#include<fstream>
#include<stdio.h>
//#include<cstd.lib>
#include<sys/wait.h>
#include<sys/types.h>
#include<unistd.h>
using namespace std;
int replace(string& file_content, const string& target, const string& replacement);
int main()
{
ifstream file_read("articleTester.txt");
string content, target, replacement;
int count = 0;
//for(char ch; inputStream.get(ch); content.push_back(ch)){}
while(!file_read.eof()){
content.push_back((char)file_read.get());
}
cout << "File Content: " << content << endl;
cout << "Target Word: ";
cin >> target;
cout << "Replacement: " ;
cin >> replacement;
count = replace(content, target, replacement);
cout << "Count: " << count << endl;
cout << "File Content: " << content << endl;
file_read.close();
ofstream file_write("articleTester.txt");
if(!file_write.is_open()){
cout << "article not opened 2" << endl;
}
file_write << content;
file_write.close();
}
int replace(string& file_content, const string& target, const string& replacement){
int count = 0; //counter for # of replacements
auto pos = file_content.find(target);
while( pos != std::string::npos){
file_content.replace(pos,target.length(), replacement);
//search for next replacement
pos = file_content.find(target, pos);
//increment for each word you find
count++;
}
return count;
}