C语言实现ini解析函数 getPrivateProfileString

这篇博客详细介绍了如何使用C语言实现GetPrivateProfileString函数来读取ini文件中的Section和Key值。文章讨论了函数的语法、参数、返回值,并给出了代码示例和测试用的ini文件内容。作者还提到了对原代码的修改和优化。

这是一个能够读取ini文件中的 Section Name 和各个KEY 值的函数。自知多有纰漏,还望指正。

(注:之前的代码过于繁琐。在下已于1.27 进行了修改。简化重复代码和注释,减少了超过30%的代码量。)



函数的具体定义参考MSDN定义:

https://msdn.microsoft.com/en-us/library/ms724353(VS.85).aspx

程序具体功能部分摘录如下:

GetPrivateProfileString function

Retrieves a string from the specified section in an initialization file.

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

Syntax

C++
DWORD WINAPI GetPrivateProfileString(
  _In_   LPCTSTR lpAppName,
  _In_   LPCTSTR lpKeyName,
  _In_   LPCTSTR lpDefault,
  _Out_  LPTSTR lpReturnedString,
  _In_   DWORD nSize,
  _In_   LPCTSTR lpFileName
);


Parameters

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. 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.
lpDefault [in]

A default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. If this parameter is NULL, the default is an empty string, "".

Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedStringbuffer to strip any trailing blanks.

lpReturnedString [out]

A pointer to the buffer that receives the retrieved string. 

nSize [in]

The size of the buffer pointed to by the lpReturnedString parameter, in characters.

lpFileName [in]

The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.

Return value

The return value is the number of characters copied to the buffer, not including the terminating null character.

If neither&nbs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值