有关INI文件读取的疑问.

有关INI文件读取的疑问.

问题描述:



如果在INI文件中找不到相应的条目,则GetPrivateProfileString() API将返回默认值.我想将其作为错误案例(缺少部分).使用GetPrivateProfileString() API有什么方法可以检测节或条目缺少的情况吗?

在此先感谢...

Hi,

GetPrivateProfileString() API returns the default value if the corresponding entry is not found in INI file. I want to get that as an error case (section missing). Is there any way to detect section or entry missing case using GetPrivateProfileString() API?

Thanks in advance...

好吧,如果将默认值设置为NULL,它将返回",如果未找到该部分则为空字符串. "很少是一个很好的节名称,您可以在返回时进行检查.

或者,您可以将默认设置设置为可以识别的内容,例如"ThisIsTheDefault",然后检查是否返回.

除了测试特定值之外,我想不出一种方法来检测它,因为该API完全没有默认值,可以向您隐藏缺失"的情况.

或者,您可以使用另一种形式的API来获取所有部分的枚举,并显式查找您关心的部分,并在未在枚举中找到它时检测到它丢失了.

http://msdn.microsoft.com/zh-CN /library/windows/desktop/ms724353(v=vs.85).aspx [
Well, if you set the default to NULL, it will return "", the empty string if the section isn''t found. "" is rarely a good section name and you can check for that on the return.

Or, you could set the default to something you''ll recognize, like "ThisIsTheDefault" and check for that coming back.

Other than testing for specific values, I can''t think of a way to detect it since the API goes out of its way to have a default to hide the "missing" case from you.

Alternatively, you could use the other form of the API to get the enumeration of all the sections and explicitly look for the one you care about and detect that it is missing when you don''t find it in the enumeration.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v=vs.85).aspx[^]


您可能应该注意函数说明顶部的注释在MSDN中.
You should probably note the comment at the top of the function description in MSDN.
引用MSDN:

注意:提供此功能仅是为了与基于Windows的16位应用程序兼容.应用程序应将初始化信息存储在注册表中.

Note: This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry.


因此,您可能不应该将其用于任何新开发...因为将来可能会不支持它.
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms724353%28v=vs.85%29.aspx [


So you probably shouldn''t be using this for any new development... as it may not be supported in the future.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx[^]