:: GetPrivateProfileString reads the entire section of the INI file

I am modifying an existing C ++ application and moving some values ​​that are currently hardcoded.

I do this with one class that will "manage" all of this and hold map<CString, CString> values ​​from the INI file.

Currently, I have to read each value separately using the ::GetPrivateProfileString function - can I somehow read the whole section instead of a single value?

Prefer not to read the file manually, but if there is a reasonable (i.e. efficient + easy to use) existing method, I am open to suggestions.

Edit: now he had to use it β€œfor real,” and the solution did pass NULL as the value of lpKeyName. Full code including return value analysis:

 char buffer[MAX_STRING_SIZE]; int charsCount = ::GetPrivateProfileString("MySection", NULL, NULL, buffer, MAX_STRING_SIZE, m_strIniPath); CString curValue; curValue.Empty(); char curChar = '\0'; for (int i = 0; i < charsCount; i++) { curChar = buffer[i]; if (curChar == '\0') { if (curValue.GetLength() > 0) HandleValue(curValue); curValue.Empty(); } else { curValue.AppendFormat("%c", curChar); } } if (curValue.GetLength() > 0) HandleValue(curValue); 

This is not trivial since it returns keys separated by a null character (EOS?), So I had to extract them using a loop like the above - share it here for all who might need it. :-)

+4
source share
3 answers

You do not need to read the file manually, but it helps to read the manual for GetPrivateProfileString :

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

+3
source

You should probably consider using Boost.PropertyTree (which provides the INI parser ):

The property tree library provides a data structure that stores an arbitrarily deeply nested tree of values ​​indexed at each level by some key. Each node of the tree stores its own values, as well as an ordered list of its subnodes and their keys. A tree provides easy access to any of its nodes using a path that is a concatenation of several keys.

In addition, the library provides parsers and generators for a number of data formats that can be represented by such a tree, including XML, INI, and JSON.

+3
source

Have you looked at GetPrivateProfileSection? http://msdn.microsoft.com/en-us/library/ms724348(VS.85).aspx

+2
source

Source: https://habr.com/ru/post/1333395/


All Articles