GetPrivateProfileString
参考:
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的更多相关文章
- INI文件的读取(C语言:GetPrivateProfileString/GetPrivateProfileInt)
INI文件格式说明 /********************************************* ini文件说明 ini文件是文本文件,由节点(Section)和键值对(key=val ...
- MFC中GetPrivateProfileString相关函数
项目中用到了这个函数,所以了解了一下,参考了一些博客: http://blog.sina.com.cn/s/blog_a599b5960101tsbk.html http://blog.csdn.ne ...
- ::WritePrivateProfileString()的用法,以及GetPrivateProfileString的用法注意事项
WritePrivateProfileString(_T("Section1"),_T("Field1"),Field,savePath); 函数说明,这是在写 ...
- C# 通过api函数GetPrivateProfileString读取ini文件,取不到值
通过api函数GetPrivateProfileString读取ini文件,取不到值,测试了好长时间,都不行 确认程序,ini文件都没有错误的情况,最后发现是ini文件编码的原因. 将ini文件的编码 ...
- WritePrivateProfileString GetPrivateProfileString 读取写 配置文件
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfi ...
- WritePrivateProfileString、GetPrivateProfileString 读写配置文件
WritePrivateProfileString 写配置文件 BOOL WINAPI WritePrivateProfileString( _In_ LPCTSTR lpAppName, _In_ ...
- 利用GetPrivateProfileString读取ini文件的字段
//INIClass读取类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- c# 使用GetPrivateProfileString 读ini数据 失败
项目中用到 GetPrivateProfileString但是使用中, 发现 无法读出 ini 配置中的值, 比如Enable_log =3 我读到的是 API设置的默认值. 网上说可能时字符集编码的 ...
- GetPrivateProfileString() 当 key 包含空格时,需要进行转义
使用 GetPrivateProfileString() 方法可以方便的读取 ini 格式文件中的内容,如: [section] tommy = worker 使用 C# 读取如下: 1. 先引入 G ...
- INI文件,WritePrivateProfileString()和GetPrivateProfileString()函数----转载
INI文件就是扩展名为“ini”的文件.在Windows系统中,INI文件是很多,最重要的就是“System.ini”.“System32.ini”和“Win.ini”.该文件主要存放用户所做的选择以 ...
随机推荐
- 网络测速神器:SpeedTest深度指南
最近在测试一个项目,里面涉及到一个测试case:在linux服务器上,当网络带宽较差时,观察服务的消息处理能力和表现.限制网卡带宽有许多方法,比如Wondershaper或者ethtool.那验证限速 ...
- android,在java代码中,如何给textview设置不同的颜色,以及引用color中资源设置颜色
textV.setTextColor(Color.parseColor("#FF0000")); <pre name="code" class=" ...
- python执行gradle脚本
import os import shutil import subprocess #拷贝文件 def copyFile(srcFile, dstFile): #检查源文件是否存在 if not os ...
- 关于babel你需要知道的事情
babel js转码器 ES6 ==> ES5 配置 .babelrc
- HDU-6608-Fansblog(威尔逊定理+快速乘)(多校)
Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...
- flume读取日志文件并存储到HDFS
配置hadoop环境 配置flume环境 配置flume文件 D:\Soft\apache-flume-1.8.0-bin\conf 将 flume-conf.properties.template ...
- PostgressQL
环境 ubuntu18.04 docker 中 pull postgres:10 images 连接 进入容器内部 docker exec -it [mypostgres] /bin/bash 查看p ...
- jenkins打包前端项目报 error: index-pack died of signal 15 问题解决
jenkins打包前端项目报 error: index-pack died of signal 15 问题解决 前几天用jenkins打包一个前端项目的时候出现了 error: index-pack ...
- PHP7性能提升原因
1.存储变量的结构体变小,尽量使结构体里成员共用内存空间,减少引用,这样内存占用降低,变量的操作速度得到提升 2.字符串结构体的改变,字符串信息和数据本身原来是分成两个独立内存块存放,php7尽量将它 ...
- [LeetCode] 221. 最大正方形(DP)
题目 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 ...