c语言文件解析函数ini,C语言实现ini解析函数 getPrivateProfileString

漆雕正奇
2023-12-01

这是一个能够读取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

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 lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one.

If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. In this case, the return value is equal to nSize minus two.

In the event the initialization file specified by lpFileName is not found, or contains invalid values, this function will set errorno with a value of '0x2' (File Not Found). To retrieve extended error information, call GetLastError.

Remarks

The GetPrivateProfileString function searches the specified initialization file for a key that matches the name specified by the lpKeyName parameter under the section heading specified by the lpAppName parameter. If it finds the key, the function copies the corresponding string to the buffer. If the key does not exist, the function copies the default character string specified by the lpDefaultparameter. A section in the initialization file must have the following form:

[section]

key=string

.

.

.

GetPrivateProfileString 返回值部分翻译如下:

返回拷贝到缓冲区的字符长度。不包括Null字符。

如果 lpAppName 和 lpKeyName 都不为空并且缓冲区不够大,则字符串被截取为nSize长度并且后面接NULL。并且返回值为nSize - 1

如果lpAppName或者lpKeyName是NULL,并且缓冲区不够大,则字符串被截取并且后面接两个NULL,此时返回值为 nSize - 2

如果初始化文件没有找到或者含有无效值,则该函数返回错误,为’0x2’ ;

而ini文件典型格式为:

[section]

key=string

本程序中用于测试的ini 文件内容为:

[student]

name=Stark

age=20

tel=111111

[player]

name = Lannister

age  =   30

tel=999999

[actor]

name=Depp

age=50

tel=667788 运行结果见后图。其他功能比如输入参数等可以自行完善。

附源代码:

并附用于测试的文件内容:

[student]

name=Stark

age=20

tel=111111

[player]

name = Lannister

age  =   30

tel=999999

[actor]

name=Depp

age=50

tel=667788

本程序部分参考了@EMeiMountainMonkey 的博文程序,并作出了修改和完善。

原地址:       http://blog.csdn.net/emeimountainmonkey/article/details/8536787

 类似资料: