i think its not very smart to use the xml reader/writer for a couple of reasons:
- the file you read has to be xml format
- writing the files seems to be a bitch :p
- its so easy to write your own reader/writer (scroll a bit down to see how)
- its hard to read from the file, every time you read from it you have to know exactly where the code you want is.
-...
Writing your own reader/writer:
I'm not going to explain everything, just the basics, if you want a more advanced explanation, googled it up
.
The best way to show it is with an example so here we go:
Code: Select all
#include <iostream> //we need this for cout and cerr
#include <string> //we need this to use strings
#include <fstream> //we need this to read from the file
using namespace std;
int main()
{
string word;
//an empty string to put data from the file in
char filename[] = "config.cfg";
//a string (in the form of a char with an array) with the filename
string object("bitrate");
//this is the object we are going to search for
int bitrate;
/*Here we will store the attribute of the bitrate. Because we know it will be 16, 32 or maybe 64, we use an int. If you want to have a bool, you should use a string and make a little if statement*/
ifstream inputfile(filename);
//we declare inputfile so we can then read from it
if(!inputfile.is_open()) //we check if inputfile is open
{
cerr<<"ERROR; Couldn't open "<< filename <<endl;
//if its not open we show an error
system("pause");
//pause it so people can read it
exit(1);
//and exit
}
/*now we make a while loop so that it inputs a word into our string: word. Every time it does the while loop, it enters the next word into our string starting with the first word if the word is the object we are looking for, we exit the file.*/
while(word != object)
{
inputfile >> word;
}
inputfile >> word;
//now we just need to load in the attribute, which is the next word
cout << word <<endl;
//for the sake of this example, we output it to the screen
system("pause");
return 0;
}
Now, here is my config.cfg file :
Code: Select all
driver opengl
resX 1024
resY 768
bitrate 32
fullscreen true
as you can see, easy to write and easy to read.
all you have to do is make it into a class and enjoy
btw, if you want to write from a file, you can use ofstream instead of ifstream and then do outputfile<<word;
so you just add word to the outputfile.
Good luck and have fun with it
You will never have does XML bugs, ever!!!