-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetmyrc.h
64 lines (61 loc) · 1.4 KB
/
setmyrc.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
63
64
#include"utils.h"
using namespace std;
void setShell(string key, string value){
fstream inFile;;
inFile.open("myrc.txt");
if(!inFile){
cerr << "Unable to open file";
exit(1);
}
string x;
int flag = 0;
string new_string;
while(getline(inFile,x)){
int pos = x.find("=");
if(pos>=0){
string sub1 = x.substr(0,pos);
if(sub1.compare(key)==0){
flag = 1;
new_string += (sub1+"="+value);
}
else{
new_string += x;
}
new_string += '\n';
}
}
if(flag==0){
new_string += (key+"="+value+"\n");
}
inFile.close();
fstream inFile1;
inFile1.open("myrc.txt",ios::trunc| ios::out | ios::in);
if(!inFile1){
cerr << "Unable to open file";
exit(1);
}
inFile1<<new_string;
inFile1.close();
}
string get(string key){
ifstream inFile;;
inFile.open("myrc.txt");
if(!inFile){
cerr << "Unable to open file";
exit(1);
}
string x;
while(getline(inFile,x)){
int pos = x.find("=");
if(pos>=0){
string sub1 = x.substr(0,pos);
if(sub1.compare(key)==0){
string sub = x.substr(pos+1);
inFile.close();
return sub;
}
}
}
inFile.close();
return NULL;
}