1

I have created a .ini file that looks as follows:

[one]
heading=" available state";
name=['A', 'D', 'H'];

[two]
type= ["on", "off", "switch"];

The main C program to access this ini file looks like this:

#include <stdio.h>
#include <Windows.h>

int main ()
{

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

   char returnValue1[100];
   char returnValue2[100];
   GetPrivateProfileString("states", "title", 0, returnValue1, 100, ini);
   GetPrivateProfileString("reaction", "reac_type", 0, returnValue2, 100, ini);

   printf(returnValue2[10]);

   printf("%s \n" ,returnValue1);
   printf(returnValue2);



   return 0;

}

I am able to display the whole heading from setion one and also the whole array name. But instead of displaying the whole array (name) like this

['A', 'D', 'H']; 

I just want to display the term 'A'. Similarly for the section two instead of this

["on", "off", "switch"];

I just want to diaply the "on". I cannot figure out a way to do this. Can somebody help me out please?

Anamika Ana
  • 165
  • 1
  • 3
  • 11
  • So called [INI files](https://en.wikipedia.org/wiki/INI_file) do not have "arrays" or lists or any such data. An "array" like you show is application-specific, and you need to parse it yourself. – Some programmer dude Jul 14 '15 at 08:31
  • @Joachim Pileborg: Thanks for ur reply. But Could you tell me how do I parse it ? Thanks in advance .. – Anamika Ana Jul 14 '15 at 08:36

2 Answers2

2

INI files are pretty simple, there isn't anything like an array then you have to split that string by yourself.

Fortunately it's pretty easy (let me assume we can omit [ and ] because they don't add anything for this example):

char* buffer = strtok(returnValue1, ",");

for (int i=0; i <= itemIndex && NULL != buffer; ++i) {
    buffer = strtok(NULL, ",");
    while (NULL != buffer && ' ' == *buffer)
        ++buffer;
}

if (NULL != buffer) { // Not found?
    printf("%s\n", buffer);

    if (strcmp(buffer, "'A'") == 0)
        printf("It's what I was looking for\n");
}

For string trimming (bot for [, spaces and eventually quotes) you may use code from How do I trim leading/trailing whitespace in a standard way?.

(please note that code is untested)

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • I think you'd better call the 'trimming' loop and print `buffer` (or save it somehow) before getting the next token (otherwise you would do nothing with the first one). The `printf()` after the loop invokes UB because `buffer` is most probably `NULL` here – Ingo Leonhardt Jul 14 '15 at 09:20
  • @IngoLeonhardt it's very very illustrative, you should check for NULL but it may (should!) be not because of itemIndex (index of "vector" item you want to extract). It's to extract just one single item. – Adriano Repetti Jul 14 '15 at 09:24
  • @AdrianoRepetti: thank you for that :) I had one more qn.. Now after getting the letter 'A' , 'H' or 'D', what do I do if I need to check the contents of buffer and verify that the letter that it contains is an 'A' (probably with an if statement'). – Anamika Ana Jul 14 '15 at 10:18
  • 1
    You don't have single characters then you have to perform a string comparison, I update answer with a small example. – Adriano Repetti Jul 14 '15 at 10:19
1

One way to solve your problem is to parse it yourself (that's really the only way to do it), and one way of parsing it is something like this:

  1. Remove the leading and trailing '[' and ']' (read about the strchr and strrchr functions respectively)
  2. Split the remaining string on the comma ',' (read about the strtok function)
  3. For each sub-string, remove leading and trailing white-space (read about the isspace function)
  4. You now have the values, and can put them into a list or an array of strings
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621