:: GetPrivateProfileString读取INI文件的整个部分 [英] ::GetPrivateProfileString read whole section of INI file
问题描述
我正在修改现有的C ++应用程序,并移出一些当前硬编码的值。
I'm modifying existing C++ application and moving out some values that are currently hard coded.
我使用一个类来管理整个事情,并保持映射< CString,CString>
从INI文件的值。
I'm doing this with one class that will "manage" this whole thing and hold map<CString, CString>
of the values from the INI file.
使用 :: GetPrivateProfileString
函数分别读取每个值 - 可以以某种方式读取整个部分而不是单个值吗?
Right now I have to read each value separately using ::GetPrivateProfileString
function - can I somehow read whole section instead of single value?
喜欢不必手动读取文件,但如果有任何合理的(即高效+易于使用)现有的方式,我可以提出建议。
Prefer not to have to read the file manually, but if there's any reasonable (i.e. efficient + simple to use) existing way I'm open for suggestions.
编辑:现在不得不使用它为真实和解决方案确实传递NULL作为 lpKeyName 值。包含解析返回值的完整代码:
just now had to use it "for real" and the solution was indeed passing NULL as the lpKeyName value. Complete code including parsing the return value:
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);
这不是微不足道,因为它返回由零字符分隔的键(EOS?),所以我不得不提取他们使用循环,如上面 - 在这里分享它为了每个人的可能需要它的缘故。 : - )
It's not trivial as it returns the keys separated by zero character (EOS?) so I had to extract them using loop such as the above - share it here for the sake of everyone who might need it. :-)
推荐答案
您不需要手动读取文件,但它有助于阅读 GetPrivateProfileString :
You don't need to read the file manually but it helps to read the manual for GetPrivateProfileString:
lpKeyName [in]:关键字
的名称,其关联的字符串将被检索
。 如果此参数为NULL,
将lpAppName参数指定的
中的所有键名称复制
到
lpReturnedString参数指定的缓冲区 。
这篇关于:: GetPrivateProfileString读取INI文件的整个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!