参考:

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. 【Android】Android开发启动app弹出一张广告图片,Dialog可以查看大图,查看某个图片功能

    作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 首先给大家看一下我们今天这个最终实现 ...

  2. c# 可获取系统环境

    c# 可获取系统环境, 启动进程执等 *.shell [MenuItem("Tools/DesignHelper/Clean and Pull")]     private sta ...

  3. webstorm编写vue、react 将大驼峰组件命名转换成短横杠命名

    大家好!我是木瓜太香,精通 webstorm 与常见前端技术的工程师,偶尔也在b站搞一些 webstorm 技巧教学,今天给大家带来的是大驼峰小驼峰快速转换短横杠命名或者下划线命名的方式. 开发中我们 ...

  4. 蒲公英 &#183; JELLY技术周刊 Vol.21 -- 技术周刊 &#183; React Hooks vs Vue 3 + Composition API

    蒲公英 · JELLY技术周刊 Vol.21 选 React 还是 Vue,每个人心中都会有自己的答案,有很多理由去 pick 心水的框架,但是当我们扪心自问,我们真的可以公正的来评价这两者之间的差异 ...

  5. Python 3.8.1 各版本下载地址

    Version Operating System Description MD5 Sum File Size GPG Gzipped source tarball Source release   f ...

  6. SpringBoot打Jar包在命令行运行

    首先写一个测试文件 然后点击IDEA右侧的maven,然后选择package,之后点击上面运行或者直接双击即可, 等下方控制台构建成功即可: 然后找到项目目录下target下即可看到打的jar包 然后 ...

  7. Linux:crond(crontab)定时任务

    一..定义 Crond 是linux系统中用来定期执行命令或指定程序任务的一种服务或者软件.一般在安装完系统时,crond会默认存在. crond默认每分钟会检查系统中是否有需要执行的定时任务.如果有 ...

  8. 【小白学PyTorch】9 tensor数据结构与存储结构

    文章来自微信公众号[机器学习炼丹术]. 上一节课,讲解了MNIST图像分类的一个小实战,现在我们继续深入学习一下pytorch的一些有的没的的小知识来作为只是储备. 参考目录: @ 目录 1 pyto ...

  9. Node.js向MongoDB中插入并查询数据

    首先必须要保持Node.js与MongoDB保持连接 具体教程见:Node.js连接MongoDB数据库步骤 插入数据步骤如下 node项目文件如下:在routes文件夹下新建insert.js文件, ...

  10. oracle数据处理之exp/imp

    oracle 导出/导入数据方法一 exp/imp工具:1 将数据库oracle01完全导出,DBA:sys,密码:123456:用户名Scott 密码123456 导出到D:\emp.dmp中 ex ...