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?
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);
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.
fopen(). Check against errors.fgets(). Check for success."[states]". You may want to get rid of the trailing newline read by the fgets() first.strtok().title here. You may use strcmp().