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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: getprivateprofilestring函数是一个Windows API函数,用于从INI文件中读取指定的键值对。它可以读取INI文件中的字符串类型的键值对,并将结果存储在指定的缓冲区中。该函数通常用于读取应用程序的配置文件。 ### 回答2: getprivateprofilestring函数是一种在Windows操作系统上使用的系统函数,用于获取指定配置文件INI文件)中指定节(Section)下指定键(Key)的字符串型(String)值,该函数一般由Win32应用程序使用,主要用途是存储和获取程序的配置信息。 该函数的语法如下: ```C++ DWORD WINAPI GetPrivateProfileString( _In_opt_ LPCTSTR lpAppName, _In_opt_ LPCTSTR lpKeyName, _In_opt_ LPCTSTR lpDefault, _Out_ LPTSTR lpReturnedString, _In_ DWORD nSize, _In_opt_ LPCTSTR lpFileName ); ``` 其中,参数lpAppName、lpKeyName、lpDefault、lpReturnedString和nSize的含义如下: - lpAppName:待获取配置文件中的节名(Section),如果为NULL,则获取所有节下指定键(Key)的值; - lpKeyName: 待获取配置文件中的键名(Key),如果为NULL,则获取指定节(Section)下所有键(Key)的值; - lpDefault:在无法获取指定键(Key)的字符串型(String)值时,返回的默认值; - lpReturnedString:存储获取到的字符串型(String)值的缓冲区; - nSize:用于指定存储获取到的字符串型(String)值的缓冲区的大小; - lpFileName: 指定获取的配置文件INI文件)的路径和文件名。 当GetPrivateProfileString函数执行成功时,会返回获取到的字符串型(String)值的长度,如果执行失败,则会返回0。在实际使用该函数的过程中,需要注意参数的传递顺序和数据类型的匹配问题,否则会导致程序出错或无法正确获取数据。 总的来说,GetPrivateProfileString函数在程序的开发和维护中有着非常重要的作用,它可以方便地存储和获取程序的配置信息,降低程序开发成本和提高开发效率。 ### 回答3: getprivateprofilestring函数是Windows操作系统提供的一个用于读取INI文件中指定配置项的函数INI文件是一种配置文件格式,常用于Windows系统中保存程序设置和配置信息。该函数在程序开发中广泛用于读取INI文件中的配置信息,以方便程序根据不同的需求进行不同的操作。 getprivateprofilestring函数函数原型为: DWORD GetPrivateProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName ); 函数参数说明如下: lpAppName:指定要读取的配置项所在的节名,即INI文件中用方括号[]括起来的部分。 lpKeyName:指定要读取的配置项名,即INI文件中节名下面的键值名称。 lpDefault:指定读取失败时的默认值。 lpReturnedString:指定读取结果的缓冲区,即读取到的值将被保存在此处。 nSize:指定读取结果缓冲区大小,以确保结果可以完全存储。 lpFileName:指定要读取的INI文件名,可以包含全路径或仅包含文件名。 函数返回值为读取结果的长度,不包括空字符。 使用getprivateprofilestring函数时,需要注意以下几点: 1. 需要将Windows.h头文件包含在程序中,以便使用该函数。 2. INI文件的编写需要按照一定的格式,即每一个配置项占用一行,格式为“键名=键值”。 3. INI文件的相对路径需要与程序当前所在路径一致,否则需要指定完整的路径信息。 4. 使用该函数时,需要判断读取结果的长度是否超出了指定的缓冲区大小,以避免内存溢出问题。 总之,getprivateprofilestring函数是一个非常方便的INI文件读取工具,在程序开发中经常被使用,可大大简化开发工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值