参考:

1. https://blog.csdn.net/tunnel115/article/details/3081340

2. https://blog.csdn.net/hopedream2008/article/details/8113798

看完以下博客的一个小例子:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include "stdafx.h"
#include"afx.h"
#include"iostream" int _tmain(int argc, _TCHAR* argv[])
{
CString strStudName;
GetPrivateProfileString(_T("StudentInfo"), _T("Name"), _T("默认姓名"), strStudName.GetBuffer(MAX_PATH), MAX_PATH, _T("D:\\aa\\ConsoleApplication4\\Debug\\student.ini"));
std::cout << strStudName << std::endl;
system("pause");
return 0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

ini 里面文件:

[StudentInfo]
Name = Aaron

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

控制台运行结果:


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

有关GetPrivateProfileString的使用方法
======================================================

函数返回值为string的长度(long型),而从ini文件获得的字符串则保留在目的缓冲器中

DWORD GetPrivateProfileString(
LPCTSTR lpAppName, //配置文件的section名
LPCTSTR lpKeyName, //配置文件的key名
LPCTSTR lpDefault, 
LPTSTR lpReturnedString, 
DWORD nSize, 
LPCTSTR lpFileName 
); 

  其中各参数的意义: 

   前二个参数与 WritePrivateProfileString中的意义一样.

   lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量. 

   lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.

   nSize : 目的缓存器的大小.

   lpFileName : 是完整的INI文件名.

下面是一个常见的出错原因:
GetPrivateProfileString怎么总是读不出来
--------------------------------------------------------------------------------

*.INI内容
[NETWORK]
ServerIP=100.100.100.53
程序:
main()
{
char ip[16];
DWORD num=0;
num=GetPrivateProfileString("NETWORK","ServerIP,"",
       ip,sizeof(ip),
   "Server.ini");
cout<<num<<endl<<ip<<endl;
}

--------

num=GetPrivateProfileString("NETWORK","ServerIP,"",
       ip,sizeof(ip),
   "Server.ini");
Server.ini这个文件放在哪里的?要放在与应用程序相同的目录下应该用".//server.ini"
你看看是不是没有找到这个INI文件

-----------

VC中调试时,server.ini放在工程目录中;程序单独运行时,则需要放在跟exe同一个目录中。
因为从VC里启动程序,VC将程序的工作目录初始化为工程目录,而不是debug或release目录本身。
 

函数作用:从INI文件中读入程序中的变量.

1.所用的WINAPI函数原型为:DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
其中各参数的意义:
前二个参数与 WritePrivateProfileString中的意义一样.
lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.

2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");
执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".  

3.读入整型值要用另一个WINAPI函数:
UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);
  这里的参数意义与上相同.使用方法如下:
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");




GetPrivateProfileString的更多相关文章

  1. INI文件的读取(C语言:GetPrivateProfileString/GetPrivateProfileInt)

    INI文件格式说明 /********************************************* ini文件说明 ini文件是文本文件,由节点(Section)和键值对(key=val ...

  2. MFC中GetPrivateProfileString相关函数

    项目中用到了这个函数,所以了解了一下,参考了一些博客: http://blog.sina.com.cn/s/blog_a599b5960101tsbk.html http://blog.csdn.ne ...

  3. ::WritePrivateProfileString()的用法,以及GetPrivateProfileString的用法注意事项

    WritePrivateProfileString(_T("Section1"),_T("Field1"),Field,savePath); 函数说明,这是在写 ...

  4. C# 通过api函数GetPrivateProfileString读取ini文件,取不到值

    通过api函数GetPrivateProfileString读取ini文件,取不到值,测试了好长时间,都不行 确认程序,ini文件都没有错误的情况,最后发现是ini文件编码的原因. 将ini文件的编码 ...

  5. WritePrivateProfileString GetPrivateProfileString 读取写 配置文件

    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfi ...

  6. WritePrivateProfileString、GetPrivateProfileString 读写配置文件

    WritePrivateProfileString 写配置文件 BOOL WINAPI WritePrivateProfileString( _In_ LPCTSTR lpAppName, _In_ ...

  7. 利用GetPrivateProfileString读取ini文件的字段

    //INIClass读取类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  8. c# 使用GetPrivateProfileString 读ini数据 失败

    项目中用到 GetPrivateProfileString但是使用中, 发现 无法读出 ini 配置中的值, 比如Enable_log =3 我读到的是 API设置的默认值. 网上说可能时字符集编码的 ...

  9. GetPrivateProfileString() 当 key 包含空格时,需要进行转义

    使用 GetPrivateProfileString() 方法可以方便的读取 ini 格式文件中的内容,如: [section] tommy = worker 使用 C# 读取如下: 1. 先引入 G ...

  10. INI文件,WritePrivateProfileString()和GetPrivateProfileString()函数----转载

    INI文件就是扩展名为“ini”的文件.在Windows系统中,INI文件是很多,最重要的就是“System.ini”.“System32.ini”和“Win.ini”.该文件主要存放用户所做的选择以 ...

随机推荐

  1. SpringCloud Alibaba Nacos 服务注册

    业务服务接入Nacos服务治理中心 启动Nacos访问地址为:http://101.200.201.195:8848/nacos/ 创建bom工程用于管理依赖(下方附加源码地址) 准备工作完成后开始接 ...

  2. JavaScript 究竟是怎样去执行的?

    摘要: 理解 JS 引擎运行原理. 作者:前端小智 原文:搞懂 JavaScript 引擎运行原理 Fundebug经授权转载,版权归原作者所有. 一些名词 JS 引擎 — 一个读取代码并运行的引擎, ...

  3. unity shader

    Unity新的shader叫做 基于物理渲染的shader,先介绍下新的shader具有的特性: Energy Conservation(能量守恒):这是一个基于物理的概念,它确保物体反射的光线不会比 ...

  4. 13_Python的面向对象编程-类class,对象object,实例instance

    1.面向对象概述 1.类是用来描述对象的工具,把拥有相同属性和行为的对象分为一组     2.对象是由类实例化出来的一个具体的对象         属性: 对象拥有的名词,用变量表示         ...

  5. github学生认证——申请学生开发包

    写在前面 申请学生认证的好处: GitHub学生的免费AWS Educate入门帐户,价值100美元. 专业的桌面IDE:IntelliJ IDEA,PyCharm等.学生的免费订阅,每年更新一次. ...

  6. The relationship between Sonarcube coverage and code branch

    Once I was asked to enhance the sonarcube coverage of the class:‘jp.co.XXXXp.DltApiHttpRequestRetryH ...

  7. JavaScript函数及面向对象

    函数及面向对象 目录 函数及面向对象 1. 定义函数 1. 定义方式一 2. 定义方式二 2. 调用函数 1. 参数问题 2. arguments 3. rest 3. 变量的作用域 1 . var的 ...

  8. sqlserver语句的执行顺序

    执行顺序: 1.from 2.where 3.group by 4.having 5.根据select 关键之后的要显示的字段,进行结果集显示 6.order by 对最终结果集进行排序 7.top/ ...

  9. Linux:apache安装

    1.查询是否已安装 rpm -qa httpd 如果已安装,先卸载 发现有依赖包,先把依赖卸载 或者加上--nodeps参数,不考虑依赖,直接卸载   rpm -e --nodeps httpd-2. ...

  10. 喵的Unity游戏开发之路 - 多场景:场景加载

    如果丢失格式.图片或视频,请查看原文:https://mp.weixin.qq.com/s/RDVMg6l41uc2IHBsscc0cQ 很多童鞋没有系统的Unity3D游戏开发基础,也不知道从何开始 ...