Link to home
Start Free TrialLog in
Avatar of Jako PH
Jako PH

asked on

C++ Getting section names

Hi :)
This is my first post here and i hope to get an help :)
Anyway let's start with asking my question
my problem was that i only get 1 section name when using getprivateprofilesectionnames function
so i have searched a lot for an solution and i already found an solution but i don't actually understand how that works so could some one explain what's happening in the loop please ?

int main()
{
	char buffer[4096];
	GetPrivateProfileSectionNames(buffer, 4096, ".//SectionO.ini");

	char* Get = (char*)buffer;
	size_t length = 0;
	int nCnt = 0;
	while (*Get)
	{
		length = strlen(Get);
		cout << nCnt << Get << endl;
		nCnt++;
		Get += length;
		Get++;
	}
    return 0;
}

Open in new window


^ this is the code i have found can some one please explain what's happening in the loop ?
thanks guys.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
	char* Get = (char*)buffer;  // Get points to begin of buffer
	size_t length = 0;
	int nCnt = 0;
	while (*Get)   // while the character Get points to is not zero 
	{
		length = strlen(Get);    // determine the length what is equivalent to looking for the next zero
		cout << nCnt << Get << endl;  // output the string Get currently is pointing to
		nCnt++;                                // increment the counter
		Get += length;                     // Get now points to the zero character 
		Get++;                                  // Get now points to the next character after Zero
                                                      // if this char is not zero the loop continues
	}
 

Open in new window


Sara
Avatar of Jako PH
Jako PH

ASKER

Thanks for your effort by there's something i still can't understand it
at the first while loop ... say that the first section name is = "Section"
now the string length of Section = 8
and since the length equals the string length of the buffer then the length is also equals 8
Now when we did this Get += length
Since Get = 8 and length = 8 then how this became 0 ?  mustn't it be 14 ?
and if we increment the get it should be 1 :/
Sorry i don't understand this i am sorry.
Now when we did this Get += length
Since Get = 8 and length = 8 then how this became 0 ?  mustn't it be 14 ?


assume the buffer contains

SECTA|SECTB|SECTC|SECTX||

Open in new window


the | stands for a binary zero character (character code is 0. don't mix it up with '0' what has code 48).

the buffer contents is

 0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4    byte position
----------------------------------------------------------------------------
 S  E  C  T  A     S  E  C  T  B     S  E  C  T  C     S  E  C  T  X          Text
83 69 67 84 65 00 83 69 67 84 66 00 83 69 67 84 67 00 83 69 67 84 88 00 00    decimal

Open in new window


- at begin Get points to the 'S' of SECTA
- strlen(Get) == 5 since the first binary zero character terminates the string length
- Get += 5 makes that Get points to the separating zero at byte position 5   (positions are 0-based, so it is the 6th character).
- Get++ makes that Get points to the S of SECTB
- .... and so on ....
- Get++ makes that Get points to the S of SECTX
- Get += 5 makes that Get points to the separating zero at byte position 23
- Get++ makes that Get points to the second binary zero after SECTX

the last pointer makes that while(*Get) is false and the loop breaks.

Sara
Avatar of Jako PH

ASKER

Thanks a lot Sara :)
Now i wanted to ask does GetPrivateProfileString reads also the section names or it reads the whole file ?
because i tried it and it just reads the section names also
Avatar of Jako PH

ASKER

	char buffer[10000];
	char *Get2 = (char*)buffer;
	int Lsize = sizeof(buffer);
	int length = 0;
	int counter = 0;
	GetPrivateProfileString(NULL,NULL,NULL,buffer,Lsize,".//test.ini");
	while (*Get2){
        length = strlen(Get2);
        cout << counter << " | " << Get2 << endl;
        counter++;
        Get2 += length;
        Get2++;
	}

Open in new window


this is the code ^
and this is what it displays :

User generated image
and the is containing :

[Section1]
KeyTesting 
=Hello 

[Section2]
KeyTesting 
=Hello 

[Section3
]
KeyTesting 
=Hello 

[Section4
]
KeyTesting 
=Hello 

[Section5
]
KeyTesting 
=Hello 

[Section6]
KeyTesting 
=Hello 

[Section7
]
KeyTesting 
=Hello 

[Section8
]
KeyTesting 
=Hello 

[Section9]
KeyTesting 
=Hello 

[Section10]
KeyTesting 
=Hello 

Open in new window

GetPrivateProfileString reads one single string entry from inifile.

you were passing the filename, the section name and the entry name what allows to get a text from a file "abc.ini" with a Contents

[SECTA]
Entry1=abcdefg
Entry2=xyz
Entry3=12345

[SECTB]
Entry4=4

[SECTC]
Entry5=%
Entry6=kkkk lllll mmmm nnnnooo

[SECTX]
EntryX=xxxxx xxxxx xxxxx
EntryY=yyy
EntryZ=aaaa

Open in new window


by code like


char returnstring[128] = { '\0' };
GetPrivateProfileString("SECTX", "EntryY", "", returnstring, sizeof(returnstring), "c:\\temp\\abc.ini");

Open in new window


Sara
Avatar of Jako PH

ASKER

here it says if the parameter is NULL it copies everything to the buffer :
GetPrivateProfileString MSDN

read my old comment
i never tried to pass NULL pointers for section, entry, and Default. but obviously the reurned string it is equivalent to that of GetPrivateProfileSectionNames if you do so.

i would guess if you pass a valid section name and let the entry name NULL you would get all entries to the given section (though there should be a separate function available as well).

Sara
"everything" is not possible, as the entries and sections build a tree hierarchy.

the docs say

lpAppName [in]

    The name of the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.


lpKeyName [in]

    The name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.

that is what i guessed.

Sara
Avatar of Jako PH

ASKER

anyway i just wanted to read all the section names and i did that now i wanted to read all the keys in all the sections in the file how can i do that ?
as told, you pass the sectionname in the first argument and NULL for the second (and third) argument.

then you either get a list of all entry keys or (if i rightly remember) a list of entryname=entryvalue pairs separated by zeros same as the section names.

if you only get the keys you would need a further call of GetPrivateProfileString (and a second buffer for the value) to get the value.

Sara