-
Notifications
You must be signed in to change notification settings - Fork 0
/
erase.h
63 lines (56 loc) · 1.32 KB
/
erase.h
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
#include <iostream>
#include <fstream>
using namespace std;
void eraseFileLine(std::string path, std::string eraseLine) {
std::string line,str;
std::ifstream fin;
fin.open(path);
std::ofstream temp; // contents of path must be copied to a temp file then renamed back to the path file
temp.open("temp.txt");
int j=0;
int count=0;
int line1;
char buf[512];
while (std::getline(fin, str))
{
count++;
if(count%2==0)
{
istringstream iss(str);
string word;
while(iss >> word)
{
int i;
for(i=0;i<word.length();i++)
buf[i]=word[i];
buf[i]='\0';
string s2=buf;
if(s2==eraseLine)
line1=count;
}
j=0;
}
}
fin.close();
fin.open(path);
count=0;
while (getline(fin, line))
{
count++;
if (count==line1-1)
{
j=1;
continue;
}
if(j==1)
{j=0;
continue;
}
temp << line << std::endl;
}
temp.close();
fin.close();
const char * p = path.c_str(); // required conversion for remove and rename functions
remove(p);
rename("temp.txt", p);
}