c++读取配置文件函数GetPrivateProfileString()

-----------------英语不好可以跳过看最后的例子-------------------------------

MSDN原文:
GetPrivateProfileString()
The 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.

DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpDefault, // default string
LPTSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // initialization file name
);
Parameters
lpAppName
[in] Pointer to a null-terminated string that specifies 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.

lpKeyName
[in] Pointer to the null-terminated string specifying 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] Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. This parameter cannot be NULL.
Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.

Windows 95/98/Me: Although lpDefault is declared as a constant parameter, the system strips any trailing blanks by inserting a null character into the lpDefault string before copying it to the lpReturnedString buffer.

Windows NT/2000/XP: The system does not modify the lpDefault string. This means that if the default string contains trailing blanks, the lpReturnedString and lpDefault strings will not match when compared using the lstrcmp function.

lpReturnedString
[out] Pointer to the buffer that receives the retrieved string.
Windows 95/98/Me: The string cannot contain control characters (character code less than 32). Strings containing control characters may be truncated.

nSize
[in] Specifies the size, in TCHARs, of the buffer pointed to by the lpReturnedString parameter.

lpFileName
[in] Pointer to a null-terminated string that specifies 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 Values
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.
/—如果lpAppName和lpKeyName都不为NULL且提供的目标缓冲区太小而无法保存请求的字符串,则字符串将被截断,后跟空字符,返回值等于nSize减1。----/

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.
/—如果lpAppName或lpKeyName为NULL且提供的目标缓冲区太小而无法容纳所有字符串,则最后一个字符串将被截断,后跟两个空字符。 在这种情况下,返回值等于nSize减去2。—/

实例:
现有配置:EmpCommonPrint.ini内容如下:
在这里插入图片描述
代码如下:

CHAR buf[1024]={0};
GetPrivateProfileString(“base”,“xmlFile”,"",buf,MAX_PATH,“E:\XD\2_project\4_菲律宾采集箱\外设测试\佳能打印机\彩色打印机测试\test\test\EmpCommonPrint.ini”);

此时buf存放的内容就是我们需要加载的打印机模板的全路径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值