GetPrivateProfileSectionNa
Why you don't want to take any ini-file parser from the internet?
http://www.codegu
Hi experts, how can i enumerate all sections from an ini file. A snippet would be really nice. The number of the sections is unknow and the number is also unknown. Also each section have a few keys named "key1", "key2" etc...
Thanks in advance!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
GetPrivateProfileSectionNa
Why you don't want to take any ini-file parser from the internet?
http://www.codegu
All the section names are terminated with a '\0'. So if you print the buffer you get only the first. The last name is terminated with two '\0';
for example ( two sections, "FIRST" and "LAST"):
buf[0] = 'F'
buf[1] = 'I'
buf[2] = 'R'
buf[3] = 'S'
buf[4] = 'T'
buf[5] = 0 <----- 1 terminator
buf[6] = 'L'
buf[7] = 'A'
buf[8] = 'S'
buf[9] = 'T'
buf[10] = 0 <----
buf[11] = 0 <----- 2 terminators
>>>> So, I'd say, this API is very old and better use something else.
It is older than 17 years. That was when I started with VC1.1 at Windows 3.1. But most functions of WINAPI have the same age. And the method to return a list of strings whithin one string separated by single zero chars and a final double zero was used in many other WINAPI functions as well, e. g. the filenames selected when using OpenFileDialog were returned same way. BTW, you can use the strtok function to parse such kind of buffer.
char* p = strtok(s, "\0");
do
{
cout << p << endl;
p = strtok(p + strlen(p) + 1, "\0");
}
while (p != NULL);
what is the way MFC source code is parsing such kind of 'dynamic string arrays'.
I don't think that a younger API necessarily was better? I myself made a inifile class in a portable project where they already had a inifile class which was poorly bad. In OS/2 they had a infile class where the maximum entry length was limited to 100 chars. Also the entries per section were limited. None of those classes had a function to retrieve all sections or all entries of a given section.
>>>> It is "only for compatibility with 16-bit Windows-based applications".
No. It is part of WINAPI and was used in the youngest versions of VC as well. The only point is that nowadays projects probably would use XML files rather than inifiles. But even that must not really be an advantage.
>>>> It is "only for compatibility with 16-bit Windows-based applications"
In my opinion this sentence was written in MSDN in the past, when Windows 95 was released; at that time Microsoft tought that all applications should put their configuration data in the system registry. As we learn some years later, system registry is not a good solution for all kind of informations (COM teach...)... a separate private file is often better!
Sure, as itsmeandnobodyelse said, may be that XML is more modern and powerfull, but for simple application configuration, INI files are still a convenient, and often easy (to read, write...) way to go.
Yes, you're right. But I took "only for compatibility with 16-bit Windows-based applications" from MSDN.
How I remember in 2000 this API (or only MFC?) worked only with 16K ini-file.
Everybody has own ini-file, xml-parser, ...
I'd say, sometimes it is even dangerous - I just continue with your idea. These "home-made" parsers should be at least faster than their standard implementations. And use less memory.
I have to use "home-made" implementations, because I do a lot of work for the windows mobile/CE devices. So when you say, for example, to try to avoid the pointers... I cannot. I have only 32MB per process.
To make this comment more related to the question :), i will say that I hope that strlen is faster then strtok. Also you call strtop more often then I did in my "strlen" example. And, anyway, you use strlen. :)
Funny discussion about a very simple question. Nice to talk with you. Thanks.
>>>> all applications should put their configuration data in the system registry
They did both. You could have your inifile in registry and/or as a file on the disk. Win.ini and system.ini still are in registry (and still working as they did in Win3.1 in many options). I could bet that putting a win.ini to windows folder would override the settings which are in registry.
pgnatyuk is right. The accepted answer should fit to the initial question in order to make the thread useful for the PAQ database. In your final code you used GetPrivateProfileSectionNa
>>>> The code was partial, but this is the mos inportana part....
That's why you can make those answers which helped additionally assisted answers. You can award points freely between accepted and assisted answers depending on their importance for you.
If you would use the 'Request Attention' link located in the initial question, a Moderator would reopen the thread on your request.
Thanks, Alex
Business Accounts
Answer for Membership
by: alb66Posted on 2009-09-02 at 05:23:00ID: 25240178
You can use GetPrivateProfileString with the first parameter = NULL en-us/libr ary/ ms7243 53%28VS.85 %29.aspx
http://msdn.microsoft.com/
GetPrivateProfileString( NULL, NULL, NULL, lpReturnedString, nSize, lpFileName );
From MSDN:
If lpAppName is NULL, GetPrivateProfileString copies all section names in the specified file to the supplied buffer. If lpKeyName is NULL, the function copies all key names in the specified section to the supplied buffer. An application can use this method to enumerate all of the sections and keys in a file. In either case, each string is followed by a null character and the final string is followed by a second null character. If the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters.