I have a custom .Ini file that looks like this :

[Main props]
lives=3
name=danny
[Mommy props]
lives=2

I want to know how to read from the "Main props" section and assign a new value to the "lives" key and also reading from the "name" key.

I dont know how to go on about identifying the section, and changing its key.
If i just change the string values then ill have a problem when i change the "Main props" lives cause it will automatically change "Mommy props" lives and thats not what i want.
so i am striving to make a function like "ChangeIniValue(section, key, new value)

I am using windows CLR forms but i dont think it will complicate this issue.

You can use the registry functions for *.ini files. See this link, then scroll down the page until you find GetPrivateProfileInt() and etc.

Or you could just write your own functions to read/write/update the *.ini file. Since *ini files are just text files you will have to rewrite the entire file in order to change anything. If you have a lot of changes to be made at one time then it will be a lot faster to do it yourself rather than use the win32 api registry functions.

commented: Perfect answer. +1

You should see if there is an XML parser library for c++. This is pretty much exactly the idea of XML. Otherwise, just read in line by line, check for '[', then keep reading all of the lines until you hit another '['.

Go with the link that Ancient Dragon provided.

Ok seeing "GetPrivateProfileString" is the best way now i just wanna ask a few questions on the parameters which is :
lpReturnedString and nSize, i dont completly understand there purpose.

lpReturnedString is a buffer in which the function will store the string that you want. You have to declare the buffer, and the function will fill it in.

Example: You have to replace "SectionName" and "KeyName" with appropriate values that are in your *.ini file. Read the Remarks section for that function for more information and examples.

char buf[255];
DWORD nCharacters = GetPrivateProfileString("SectionName", "KeyName", NULL, buf, sizeof(buf), "c:\\mydir\\myfile.ini");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.