windows ::GetPrivateProfileString 读取 INI 文件的整个部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4534048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
::GetPrivateProfileString read whole section of INI file
提问by Shadow Wizard is Ear For You
I'm modifying existing C++ application and moving out some values that are currently hard coded.
我正在修改现有的 C++ 应用程序并移出一些当前硬编码的值。
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.
我正在用一个类来“管理”整个事情并保存map<CString, CString>
INI 文件中的值。
Right now I have to read each value separately using ::GetPrivateProfileString
function - can I somehow read whole section instead of single value?
现在我必须使用::GetPrivateProfileString
函数分别读取每个值- 我可以以某种方式读取整个部分而不是单个值吗?
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.
宁愿不必手动阅读文件,但如果有任何合理的(即高效+简单易用)的现有方式,我愿意征求建议。
Edit: just now had to use it "for real" and the solution was indeed passing NULL as the lpKeyNamevalue. Complete code including parsing the return value:
编辑:刚才不得不“真正地”使用它,解决方案确实将 NULL 作为lpKeyName值传递。包括解析返回值的完整代码:
char buffer[MAX_STRING_SIZE];
int charsCount = ::GetPrivateProfileString("MySection", NULL, NULL, buffer, MAX_STRING_SIZE, m_strIniPath);
CString curValue;
curValue.Empty();
char curChar = '##代码##';
for (int i = 0; i < charsCount; i++)
{
curChar = buffer[i];
if (curChar == '##代码##')
{
if (curValue.GetLength() > 0)
HandleValue(curValue);
curValue.Empty();
}
else
{
curValue.AppendFormat("%c", curChar);
}
}
if (curValue.GetLength() > 0)
HandleValue(curValue);
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. :-)
这不是微不足道的,因为它返回由零字符(EOS?)分隔的键,所以我不得不使用上面的循环来提取它们 - 为了每个可能需要它的人,在这里分享它。:-)
采纳答案by Eugen Constantin Dinca
You don't need to read the file manually but it helps to read the manual for GetPrivateProfileString:
您不需要手动阅读文件,但阅读GetPrivateProfileString手册会有所帮助:
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.
lpKeyName [in] :要检索其关联字符串的密钥的名称。如果此参数为 NULL,则 lpAppName 参数指定的部分中的所有键名都被复制到 lpReturnedString 参数指定的缓冲区中。
回答by icecrime
You should probably consider the use of Boost.PropertyTree(which provides a INI parser) :
您可能应该考虑使用Boost.PropertyTree(它提供了一个INI 解析器):
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 value, plus an ordered list of its subnodes and their keys. The tree allows easy access to any of its nodes by means of a path, which is a concatenation of multiple 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.
属性树库提供了一种数据结构,用于存储任意深度嵌套的值树,在每个级别通过某个键进行索引。树的每个节点都存储自己的值,以及其子节点及其键的有序列表。该树允许通过路径轻松访问其任何节点,该路径是多个键的串联。
此外,该库还为可以由此类树表示的多种数据格式提供解析器和生成器,包括 XML、INI 和 JSON。
回答by kenny
Have you looked at GetPrivateProfileSection? http://msdn.microsoft.com/en-us/library/ms724348(VS.85).aspx
你看过 GetPrivateProfileSection 吗?http://msdn.microsoft.com/en-us/library/ms724348(VS.85).aspx