Hello! I looked through the documentation on file input/output but I need a little bit more light shed on this situation. How would I go about configuration file (.ini files) input/output?
first read a line with cin.getline (or std::getline) then find '=' in it and do whatever you want to do with each half. (or use sume library to do the job for you)
Once again, Standard C++ fail to provide us a good library for this. If it is only strictly <name>=<value> it will be easy but what if it goes beyond that. Then someone told me Boost::RegEx will be the library we should go for.
If he is using windows - windows have built system functions for getting vaules from *.ini
files.
The functions have names like GetProfileString or something similar.
Here I'll post some of the sections I am having trouble understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <map>
#include <string>
namespace configuration
{
....
struct data: std::map <std::string, std::string>
{
// Here is a little convenience method...
bool iskey( const std::string& s ) const
{
return count( s ) != 0;
}
};
....
Okay here for example, where the code says "<std::string, std::string>" am I supposed to replace "string" with the information I need from the .ini file?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
....
// Skip blank lines
if (begin == std::string::npos) continue;
// Skip commentary
if (std::string( "#;" ).find( s[ begin ] ) != std::string::npos) continue;
// Extract the key value
std::string::size_type end = s.find( '=', begin );
key = s.substr( begin, end - begin );
// (No leading or trailing whitespace allowed)
key.erase( key.find_last_not_of( " \f\t\v" ) + 1 );
// No blank keys allowed
if (key.empty()) continue;
....
And here, where it says "if (std::string( "#;" ).find( s[ begin ] ) != std::string::npos)" am I supposed to replace "#;" with the name of the slot? and the "s[ begin ]" with the name of the section? Like
I know I'm a n00b at this >.< I just need a little bit of explanation on this particular parser. I also guess I should tell you what I am doing to help you help me. Its a text based console RPG game, and the .ini file holds the players stats (among other things, but haven't gone that far yet.)
If it is to be installed on a Windows machine, I would suggest take guestgulkan solution and call Windows API direct.
If you want it to be generic and not tied to any platform specific API, I would recommend you take a look at Boost::RegEx library. The reason being a Regular Expression parser would scale well in future if you decide to change your config file format again.
At this current times, most config files have changed to XML format. It seems you are still using the old Windows specific INI file format. If it is a new text based console RPG game, it is a good opportunity to change from INI to XML format IMHO.
Then you can use a C++ XML Parser to help you parse the XML-based config file.
So, for example, I could take the above snippet you posted and put into a .ini file and use GetProfileString to retreive the information?
Definitely not. INI format is based on <name>=<value> so if you use Windows GetProfileString API, you of cuz cannot retrieve those config information based on XML format.
You need to decide
1. Will the program be installed on Windows machine ?
2. Do you want a platform independent config file format ?
Then based on your decision you can choose to use Windows API or use a C++ XML parser (assume you choose XML as config file format)
2. Yes I want a platform independent configuration file format
Actually INI format can also be applied in Unix also as it is just plain text. To save you time you just use Windows API since you intend to install your program inside Windows machine.
Just note your program will then not run in Unix since the API does not exist in Unix.
int a
if (found!=string:npos) {
a = str.substr(0,found) << " " << str.substr(found+1);
}
I'm not sure if I got the syntax right, but now that I have a piece of code that I can actually work with, I will have it figured out. I learn better with a hands on approach from an example anyways.