4

I have created a basic .ini file called configuration.ini. This file looks as follows:

[Application]
no_of_applications= 4;

[states]

title = "Config File States available";
name = 'Active, Deactivated, Hot';

[reaction]
reac_type = ["switch_schedule", "activate_app", "disable_app";

Now I intend to read this .ini file from my C program. I have included the #include <configuration.ini> line in my C program. I just want to read the title from the section 'states' in the C program i.e

#include<stdio.h>
#include<configuration.ini>

int main()
{
    //read the variable 'title' from 'states' and print it
    return 0;
}

Can somebody tell me how can I do that?

a stone arachnid
  • 1,272
  • 1
  • 15
  • 27
Anamika Ana
  • 165
  • 1
  • 3
  • 11
  • 2
    You may want to consider **not** reinventing the wheel, and use [one of many existing parsers](https://github.com/ndevilla/iniparser) – Elias Van Ootegem Jul 13 '15 at 14:52
  • @EliasVanOotegem: I tried using the the iniparser. But the problem is I was not able to install it onto the Eclipse CDT platform on my Laptop. Perhaps you could send me a link where doing this has been explained? I would be thankful :) – Anamika Ana Jul 13 '15 at 14:54

2 Answers2

6

Assuming you are using Windows, you can work this way:

First include Windows.h

#include <Windows.h>

On your function, create a LPCSTR (heh!? Really? Yeah.)

LPCSTR ini = "C:\\config.ini";

And call GetPrivateProfileString:

char returnValue[100];
GetPrivateProfileString("states", "title", 0, returnValue, 100, ini);
hlscalon
  • 7,304
  • 4
  • 33
  • 40
  • Hey thanks for that.. But according to this [link](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724366%28v=vs.85%29.aspx) aren't the arguments too less for GetPrivateProfileString? – Anamika Ana Jul 13 '15 at 15:07
  • @AnamikaAna yeah. I left the return value out – hlscalon Jul 13 '15 at 16:35
  • thank you for that. :) I worked and I could print the title. I had one more question. If I wanted to print instead of the whole name i.e 'Active, Deactivated, Hot' , i just want to print the term 'Hot', how can I do that? Is it like I access an array? – Anamika Ana Jul 14 '15 at 07:18
  • @AnamikaAna You'll have to split it out based on a delimiter. Check [this](http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c) and [this](http://stackoverflow.com/questions/10349270/c-split-a-char-array-into-different-variables) – hlscalon Jul 14 '15 at 11:41
4

Well, first of all, do not #include the .ini file. It does not think what you think it does. #include <fname> actually adds the content of the file fname to the current file, which is not what you want. Remove the #include.

Secondly, be it .ini or something else, if it is having formatted text, you can always use the below algorithm to read the file.

  1. Define a buffer long enough to hold an entire line.
  2. Open the file using fopen(). Check against errors.
  3. Read a line from the file using fgets(). Check for success.
  4. Compare the read line with your section value, for example, "[states]". You may want to get rid of the trailing newline read by the fgets() first.
    • If it matches, read the next line and continue to step 6.
    • If it does not match, continue to step 3.
  5. Tokenize the input using strtok().
  6. Check the first token against your expected head value, for example, title here. You may use strcmp().
  7. If it matches, tokenize the same input string to get the next token, which is your expected value.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261