saving app setting

I want to save my app setting like this example:
[Properties]
ShowGeneralTab = 1
ShowFanTab = 1
ShowMonitoringTab = 1
ShowOnScreenDisplayTab = 1
ShowScreenCaptureTab = 1
ShowVideoCaptureTab = 1
ShowProfilesTab = 1
ShowUserInterfaceTab = 1
(got it from msiafterburner :|)

can anyone tell me how i can read it? like whenever i want to see the value of ShowGeneralTab what should i do? I already read the file tutorial on this site + tutorialspoint and still don't understand how i should do it, can anyone help me?!
hints, anything or just simply a code so i can think on it ?! :)
Hi, I don't know if there are already some libraries or other tools that do this, but you could write your own implementation: you read a line from the file and split it in two: the name of the property before the "=" and the value after the "=". Then you can check against the name of the property until you find the one you are looking for.

Maybe though there are better solutions...
If the properties of the app is an object, you can write the entire object to a file and read it back again:
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
#include <fstream>
#include <iostream>
using namespace std;

class Student {
public:
    char   FullName[40];
    char   CompleteAddress[120];
    char   Gender;
    double Age;
    bool   LivesInASingleParentHome;
};

int main() {
    Student one, two;
    strcpy(one.FullName, "Ernestine Waller");
    strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");

    one.Gender = 'F';
    one.Age = 16.50;
    one.LivesInASingleParentHome = true;
    ofstream ofs("fifthgrade.ros", ios::binary);

    // Write to a file
    ofs.write((char *)&one, sizeof(one));
    ofs.close();

    // Read object from the file
    ifstream ifs("fifthgrade.ros", ios::binary);
    ifs.read((char *)&two, sizeof(two));
    ifs.close();

    cout << "Student Information\n";
    cout << "Student Name: " << two.FullName << endl;
    cout << "Address:      " << two.CompleteAddress << endl;
    

    if( two.Gender == 'f' || two.Gender == 'F' )
        cout << "Gender:       Female" << endl;
    else if( two.Gender == 'm' || two.Gender == 'M' )
	cout << "Gender:       Male" << endl;
    else
        cout << "Gender:       Unknown" << endl;


    cout << "Age:          " << two.Age << endl;
    if( two.LivesInASingleParentHome == true )
        cout << "Lives in a single parent home" << endl;
    else
        cout << "Doesn't live in a single parent home" << endl;
    cout << "\n";

    return 0;
}

Source: http://www.functionx.com/cpp/articles/serialization.htm
thanks minomics, i thought about that, but my main problem is not understanding what to do in c++ ! i can write pesudo code but can't turn it into c++ code :| ''

smacs thats goot options for struct/classes, i can turn mine into a struct of settings ! but what if i wanted to save like 6 structs, should i make 6 files?! can't i just put them in the same place?!

but i still would ike to know how to do the it the minomics way :) + i didn't know that i can just write a class too a file like that !
Last edited on
i don't want to sound like a lazy guy but i really don't know how to do that, im really bad with strings:) i know i can check line by line, but not sure how i can only check the first part and not the = 1 , maybe i can do seekg -3 back?! and do forward again ! is that a good way?
You can start from this:

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <sstream>
#include <vector>

void split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
}


int main() {
	std::string my_string = "Property=value";
	std::vector<std::string> my_vector;

	split(my_string, '=', my_vector);

	for (auto x : my_vector) {
		std::cout << x << std::endl;
	}
}


It shows you how to split a string at a specified delimiter (in this case it's the = sign). Once you have done this, it should be fairly simple.
:O thanks, im doing it know :)
Last edited on
Ok, let us know how it goes...
strings already have split function?

No. He defined the split function at line 9.
yes i edited it, i didn't see it :)! + i didn't know getline can do that !

im super-super tired today, will do it tomorrow, but i can see how i should do that, i just simply get the line from file :) getline does the rest for me :)
Last edited on
sorry for long delay :) i got it working thanks !
Good! :)
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
using namespace std;
void split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
}


int main() {
    ifstream file("example.txt");
    vector<string> my_vector;

    string buff;

    while(getline(file,buff)){
        split(buff, '=', my_vector);

        for (auto x : my_vector) {
            cout << x << endl;
        }
    }
    file.close();
}


it starts from middle of my file and goes back to beginning and goes till end and stops, why does it get to middle first? i tried seekg(0) ! no difference!

edit: woops forgot the clear :) fixed it :D
Last edited on
Topic archived. No new replies allowed.