-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReadProperties.cpp
69 lines (59 loc) · 2.14 KB
/
ReadProperties.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
64
65
66
67
68
/**
*
* the parameter p here must using int &p
* using & in c++ grammar mean you want to change the real parameter
*/
#include "ReadProperties.h"
//read properties from file
void ReadProperties::InitProperties(char* host, char* user, char* password, char* dbName, int& p) {
//declare and open the file
fstream file(R"(D:\C++file\StuSystemGui\com\properties.txt)", ios::in);
//check if the file is open successfully
if (file.is_open()) {
//represent a line in file "properties.txt"
string line;
//using getline() function to read every line
while (getline(file, line)) {
//because the format in file like key=value,so we need to split them like dictionary
string key, value;
//get the position of '='
int pos = line.find('=');
// cout<<"pos=="<<endl;
//the ket here is a temporary variable
//it represent the value in the left next to "="
key = line.substr(0, pos);
//the '=' should be ignored
value = line.substr(pos + 1);
//init the parameter depend on the keyName
if (key == "port") {
//give value to port
//stoi function is to change a string to a int
p = stoi(value);
}
else if (key == "host") {
//give value to host
strcpy(host, value.c_str());
}
else if (key == "user") {
//give value to user
//it always mean "root"
//using c_str to change string in C++ to c_str in C
strcpy(user, value.c_str());
}
else if (key == "pw") {
//give value to password
strcpy(password, value.c_str());
}
else {
//give value to dbName
strcpy(dbName, value.c_str());
}
}
//dont forget to close the file
file.close();
}
else {
//filed to open the file
printf("failed to open file!\n");
}
}