# [C++] Parsing INI file loaded as char*

This topic is 3938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anyone help me find a library to parse char* that contains the contents of an INI file? I retrieve the file from a ZIP file and it comes out as void*.

##### Share on other sites
I'll be surprised if such a thing exists - why not just write it yourself? The ini format is incredibly straightforwards, it shouldn't take more than a couple of hours to get working.

What language? C? C++? C#?

##### Share on other sites
It seems simple, but getting all those error trappings takes time and I'm still very amatuerish in the realm of strings, character pointers, etc.

Anyhoo, I already found one that is perfect for what I need, it loads it from a file or from a string. If anyone is interested: http://code.jellycan.com/.

I've done INI parsing in .NET before, but it was so much easier with string classes and streams. C++ is kicking my butt for now so I need as much help as I can get. :)

##### Share on other sites
Quote:
 Original post by azherdevI've done INI parsing in .NET before, but it was so much easier with string classes and streams. C++ is kicking my butt for now so I need as much help as I can get. :)

Who says C++ doesn't have those things? std::string is more than adequate for strings, and <iostream> and <fstream> should do more than enough for reading files, inserting, and extracting. This is a good guide for using file streams. If you're curious as to the actual implementation of such a constructed parser, I'm sure someone can dig something up.

-jouley

##### Share on other sites
Oh, I have no doubt C++ has abundant supply of string manipulation. I am just new to it. I mean, you are talking to a guy who is still getting over the fact that I can't do this:

char *string;string = "Hello World " + "This is cool";

And I do use std::string versus char* whenever I can. I'm just not so comfortable with it yet.

##### Share on other sites
Quote:
 Original post by azherdevOh, I have no doubt C++ has abundant supply of string manipulation. I am just new to it. I mean, you are talking to a guy who is still getting over the fact that I can't do this:char *string;string = "Hello World " + "This is cool";And I do use std::string versus char* whenever I can. I'm just not so comfortable with it yet.

You can actually do that with string.

#include <string>

using namespace std;

string s;
s = "Hello World" + "Whatever";
or

s = "Hello ";
s += "World ";
s += " Whatever";

The string class methods will make it a lot easier to parse the ini file, than a char array. Because you have to be careful with the char array, and make sure you don't read over the bounds and stuff like that... just load the whole file into a string, and use the functions substr() and find().

##### Share on other sites
Quote:
 Original post by azherdevOh, I have no doubt C++ has abundant supply of string manipulation. I am just new to it. I mean, you are talking to a guy who is still getting over the fact that I can't do this:char *string;string = "Hello World " + "This is cool";And I do use std::string versus char* whenever I can. I'm just not so comfortable with it yet.

Comfort comes with practice, and the more practice you have not using char*'s, the better off you'll be. [smile] All those troubles you alluded to earlier could easily be bypassed with std::string. For example, to modify your example slightly:
std::string strTemp;strTmp = "Hello World";strTmp += ", This is cool!";std::string strFull = strTmp + " And so is std::string!";

Nearly everything you expect to be able to do you can, with much more safety and less trouble than char's. It's not a bad idea to learn about them, but using std::strings is just easier.

In the end, though, do what works!
-jouley

[Edit: Bojangled! But by good advice, so s'cool.]

##### Share on other sites
Dude, I have so much to learn, so little time. :) Thank you guys. Looks like strings will save me. I'm now reading up on them.

... I love C++ support... ;)

##### Share on other sites
Quote:
 Original post by Vampyre_Dark#include using namespace std;string s;s = "Hello World" + "Whatever";
Actually, this would not work. You are still adding two literal strings. You could do
s = s + "Hello World" + "Whatever";
or
s = string("Hello World") + "Whatever";
or
s = string()+"Hello World" + "Whatever";

or, the C way:
s = "Hello World" "Whatever";

##### Share on other sites
Quote:
 Original post by Vampyre_DarkYou can actually do that with string.#include using namespace std;string s;s = "Hello World" + "Whatever";
Not so - That'll give you an error saying you can't add two pointers.

You can however, do this:
s = std::string("Hello World") + "Whatever";
Or:
s = "Hello World" + std::string("Whatever");
Or:
s = std::string("Hello World") + std::string("Whatever");

EDIT: Bah, too slow

##### Share on other sites
I know. Quick typo. I was typing out quick pseudo code just to illustrate the point. [embarrass] I couldn't remember the proper syntax at the moment to do the first statement, so I just left it as is.

##### Share on other sites
If you really want to handle it as if it were a file, there is always istringstream in C++.

##### Share on other sites

This topic is 3938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.