Code: Select all
ID = 1
Name = a1
Password = test
hSprite = 2
aSprite = 5
Moderator: Coders of Rage
Code: Select all
ID = 1
Name = a1
Password = test
hSprite = 2
aSprite = 5
Did you mean Alvaro?JaxDragon wrote:That looks very nice. Looking for "Alex"'s source code from the comments. I need that simplicity
You could use a STL map. Read the line. Split on the "=". Store so that you have a key of "ID" and a value of "1". Then you can use some named constants to index into the map (for instance, const string ID = "ID"; const string NAME = "Name", etc) all the time, or you can extract them from the map to variables yourself (no, C++ can't go "oh, this string is Name, I need to make a string Name" or anything like that).JaxDragon wrote:As many of you know, INI files are a popular way of storing program data. The ini format I'm looking to use looks like thisand so on and so forth. What I'm having trouble with though, is reading the data into variables. I don't know how to jump to a specific line, and take the value(after the =) and put it in a variable. Any help?Code: Select all
ID = 1 Name = a1 Password = test hSprite = 2 aSprite = 5
Code: Select all
const int NUM_MAP_INDEXES = 5;
const string[NUM_MAP_INDEXES] MAP_INDEXES = {"ID", "Name", "Password", "hSprite", "aSprite"};
map<string, string> loadedValues;
/* open file ... */
string line;
string::size_type location; //pretty much just an unsigned int
getline(file, line);
while (file) {
location = line.find(" = ");
if (location != string::npos)
loadedValues[line.substr(0, location)] = line.substr(location + 1);
getline(file, line);
}
int id;
string name;
for (int i = 0;i < NUM_MAP_INDEXES; ++i) {
string val = loadedValues[MAP_INDEXES[i]];
switch (i) {
case 0:
id = atoi(val.c_str());
break;
case 1:
name = val;
break;
}
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.