4 Replies - 2037 Views - Last Post: 17 July 2010 - 10:52 PM Rate Topic: -----

#1 aaron1178   User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 170
  • View blog
  • Posts: 1,311
  • Joined: 22-October 08

C++ Console App Help

Posted 17 July 2010 - 10:29 PM

Hey,
when it comes to C++, im a total newbie. I am still in the process of creating a console app.
I want to make an ini file for it, how would i do that?
All help is appreciated. How do i do the structure of the ini file.
[test]
server = 1


Is that how i configure the ini file. how do i get the sever variable in the test section.

As i said, im total newbie, so please don't be to harsh on me.
How would i call an ini file in a c++ program

Thanks aron1178

This post has been edited by aaron1178: 17 July 2010 - 10:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Console App Help

#2 OnlineCop   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 16-April 10

Re: C++ Console App Help

Posted 17 July 2010 - 10:41 PM

There are several libraries that can do this for you (do a Google search for "ini parser c" and you'll get several results).

Now, if you want to do it yourself, you will want to establish a few things. You will want to have groups. As soon as you encounter things surrounded by square brackets, like [test], you'll need to assume that all other lines of input after that will be considered part of this group until another [header] is found.

Then, you need a "name = value" pair. Under each group, you will need to add one more "name" and "value" pair to the array of contained elements.

std::string name;
std::string value;

unsigned int index = 0;
while (input_string[index] != '=')
{
  name.push_back(input_string[index]);
  ++index;
}
// When you get here, you've reached an '='

index++; // You don't need the '=' sign; ignore it

while (index < index_string.length())
{
  value.push_back(input_string[index]);
  ++index;
}

// Now, you have a "name" and "value" pair

my_group_vector.push_back(name);
my_group_vector.push_back(value);



You could also achieve this using a std::map or std::multimap, but it really depends on whether you're doing this to learn how to do it, or if you're just doing this so you can implement an .INI file in your current project and don't care HOW you get it, as long as you get it. (In that case, I'd go with a pre-built library, where they've already done all the work, troubleshooting, and bug fixing for you.)
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++ Console App Help

Posted 17 July 2010 - 10:44 PM

I figured that windows has some built-in ini file reader but I guess not.

So you will have to parse the file yourself (or let someone else do it for you)

Its not a really difficult format to parse though.
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C++ Console App Help

Posted 17 July 2010 - 10:50 PM

GetPrivateProfileString. This function *may* not be safe to use, though it worked every time I used it (couple of times actually).
You may want to see this C++ .cfg parser. It supports the exact .cfg file format as you want:

Quote

[section]
key = value

This post has been edited by sarmanu: 17 July 2010 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 aaron1178   User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 170
  • View blog
  • Posts: 1,311
  • Joined: 22-October 08

Re: C++ Console App Help

Posted 17 July 2010 - 10:52 PM

Say i save the above as "ini_lib.h".
I Can use the following:
#include <iostream>
#include <string>
#include <ini_lib.h>

using namespace std;

int main()
{
string str;
cout << "Type yes to get ini file";
if ( str == "yes" )
{
// how do i get the ini
}
}


How do i call it, how do i define the ini file?, this is prob the most lame question, but, we all have to start somewhere right? :)

Thanks Aaron1178
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1