complicated .ini thing
What im wonderin is how to open a ini file, and look for the like:
textcolor=
so i can grab whats to the right of it. But what I want it to jump to that line, so people can rearange what line that "textcolor=" is on and it still can find it. I guess my description is kind of vauge, but I hope its enough. Thx
There are two ways you can do this, using built in Windows API calls or your own, hopefully portable, C++ code.
I''ll explain the Windows way first:
Microsoft''s Win32 API provides two functions for manipulating .INI files; the functions being GetPrivateProfileString and WritePrivateProfileString .
Lets set up an example. You have an .ini file located in a directory named C:\MyProgram. The .ini filename is settings.ini . The ''key'' you want to read is named ''textcolor'' and is located under the section ''fonts''.
Your .ini file will look like this:
Now, somewhere in your program you will make a call to GetPrivateProfileString as follows:
The variable ''buffer'' will now contain the the value ''black'' or whatever you chose to put to the right of ''textcolor=''.
You can enumerate all section names or keynames by passing a NULL value to the first or second parameters, respectively.
Hopefully this helps.
Anymore questions, feel free to ask (especially if you want the non-Win32 API specific C++ implementation)
Dire Wolf
direwolf@digitalfiends.com
I''ll explain the Windows way first:
Microsoft''s Win32 API provides two functions for manipulating .INI files; the functions being GetPrivateProfileString and WritePrivateProfileString .
Lets set up an example. You have an .ini file located in a directory named C:\MyProgram. The .ini filename is settings.ini . The ''key'' you want to read is named ''textcolor'' and is located under the section ''fonts''.
Your .ini file will look like this:
[fonts]textcolor=black
Now, somewhere in your program you will make a call to GetPrivateProfileString as follows:
TCHAR buffer[255];memset(buffer, 0, 255);DWORD result = GetPrivateProfileString("fonts", "textcolor", "defaultcolor", buffer, sizeof(TCHAR) * 255, "c:\\MyProgram\\settings.ini");
The variable ''buffer'' will now contain the the value ''black'' or whatever you chose to put to the right of ''textcolor=''.
You can enumerate all section names or keynames by passing a NULL value to the first or second parameters, respectively.
Hopefully this helps.
Anymore questions, feel free to ask (especially if you want the non-Win32 API specific C++ implementation)
Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
I tried..
and it always couts "defaultcolor", even though the settings.ini has..
any ideas?
The include i used for GetPrivateProfileString was windows.h, is that correct?
Edited by - uG on August 28, 2000 11:15:40 PM
#include #include void main(){TCHAR buffer[255];memset(buffer, 0, 255);DWORD result = GetPrivateProfileString("fonts","textcolor","defaultcolor",buffer,sizeof(TCHAR) * 255,"settings.ini");cout << buffer;}
and it always couts "defaultcolor", even though the settings.ini has..
[fonts]textcolor=black
any ideas?
The include i used for GetPrivateProfileString was windows.h, is that correct?
Edited by - uG on August 28, 2000 11:15:40 PM
I just tried it and it works fine. Hmm. Are you sure you typed in the values correctly for your .ini file.
If any of the names in the .ini file are wrong, GetPrivateProfileString(...) will return ''defaultcolor''.
Check your .ini file. I''m guessing that either the section name, [fonts], is misspelled or textcolor is misspelled.
Another thing is to make sure you are picking up the right .ini file. If you are using MS VC++ it copies the executable that is generated up one directory. For example:
C:\MyProgram\Release\Test.exe
gets moved to
C:\MyProgram\Test.exe
when you run it from inside the DevStudio IDE.
If you are still having problems let me know.
Here is the code I used to get it working properly:
- Dire
direwolf@digitalfiends.com
If any of the names in the .ini file are wrong, GetPrivateProfileString(...) will return ''defaultcolor''.
Check your .ini file. I''m guessing that either the section name, [fonts], is misspelled or textcolor is misspelled.
Another thing is to make sure you are picking up the right .ini file. If you are using MS VC++ it copies the executable that is generated up one directory. For example:
C:\MyProgram\Release\Test.exe
gets moved to
C:\MyProgram\Test.exe
when you run it from inside the DevStudio IDE.
If you are still having problems let me know.
Here is the code I used to get it working properly:
#include #include int main(){ TCHAR buffer[255]; memset(buffer, 0, sizeof(TCHAR) * 255); DWORD result = GetPrivateProfileString("fonts", "textcolor", "defaultcolor", buffer, sizeof(TCHAR) * 255, "c:\\settings.ini"); std::cout << buffer << std::endl; return 0;}
- Dire
direwolf@digitalfiends.com
Ok I got it to work, it didnt work cause of putting that function in DWORD. Now what im pondering is how to do that to grab multiple parts, like say the ini is like this...
When i tried to do it myself, i either got defaultcolor, or when I did it another way, i always got red (even tho i asked for font2)
Edited by - uG on August 29, 2000 7:03:48 PM
[fonts]font1=redfont2=black
When i tried to do it myself, i either got defaultcolor, or when I did it another way, i always got red (even tho i asked for font2)
Edited by - uG on August 29, 2000 7:03:48 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement