@Thomas1965 I like your approach. I already considered something very similar where the filename was passed in the constructor, and stored. Then when the object is destroyed, it can automatically save the map contents back to the file - if it has been modified.
@masecla33
Also in int getValue(const char * file, const char * name), why the stars? I know its something to do with pointers tough i have no idea what they do, could you explain that to me? I don't feel confortable just straight out USING code without knowing how it works, |
When an array is passed to a function, what is actually passed is a pointer to the start of the array. Hence it works the same as before.
char *
is how a character pointer is declared.
If you call the function like this:
|
int n = getValue("settings.txt", "orange");
|
the size of the string obviously isn't 255. Those literals are also read-only, which is why I put
const
in the function parameters.
you can read the tutorial about pointers here:
http://www.cplusplus.com/doc/tutorial/pointers/
also, while (fin >> key >> separator >> value) how does this work? Does fin return 0 if nothing else is left in the file? Please explain. |
That code first attempts to read from the file
fin
. Then it tests the status of the file.
You might do this in separate lines like so
1 2 3 4 5
|
fin >> key >> separator >> value;
if (!fin)
{
cout << "an error occurred";
}
|
When it is done in the loop condition, the body of the loop is executed only after a successful file access.
You already did something very similar in the function lines()
1 2
|
while (getline(myfile, line))
++number_of_lines;
|
Thank you alot! Again you are a life saver. |
You're welcome.
... but you still didn't answer the questions I asked.
Can <name> contain more than one word?
Is <value> always an integer?
edit: sorry somehow through mistiming I missed the earlier reply. sorry about that.