C++读取.ini配置文件

来源:互联网 发布:管家婆记账软件免费版 编辑:程序博客网 时间:2024/04/20 12:02

读取配置文件(.ini)

配置设置文件是Windows操作系统下的一种特殊化的ASCII文件,以"ini"为文件扩展名。通常应用程序可以拥有自己的配置设置文件来存储自己的状态信息,可以减少程序在初始化时读取配置文件时的信息量,从而可以提高程序的启动速度、提高应用程序和系统的性能,使程序更加灵活。

1、配置文件格式 ini文件由节、键、值组成

Example:

[System]    ----------------------------[Section] 节host=***.**.**.***      ----------------[键=值] key=valueusername=Userpassword=123456[TestParam]OrderSpeed=4000,10000,40000OrderTotal=120000,300000,3000000Order=10000Version=version1bakpath=/home/**localpath=C:\Users\***[picmerge]network=eth1,eth2IO=sda


2、读取配置文件:

GetPrivateProfileIntA() / GetPrivateProfileInt() 从私有初始化文件获取整型数值

GetPrivateProfileStringA()  /  GetPrivateProfileString()从私有初始化文件中获取字符串型值

WritePrivateProfileStringA() / WritePrivateProfileString() 写字符串到私有初始化文件

//MFC

CString localpath;::GetPrivateProfileStringA("TestParam","localpath","NULL",localpath.GetBuffer(100),100,IniPath); //IniPath为配置文件路径int order = GetPrivateProfileIntA("TestParam","Order",0,IniPath); //返回值即为读取到的参数WritePrivateProfileStringA("TestParam",”Version","version",IniPath); //写入字符串localpath.ReleaseBuffer();  //在对GetBuffer返回的指针使用之后需要先调用ReleaseBuffer,这样才能使用其他CString的operations/*********其它操作过程************/

//C++

LPTSTR localpath = new char[100];GetPrivateProfileString("TestParam","localpath","NULL",localpath,100,IniPath); //获取目标文件夹路径/***********************其它处理过程,如CString strPath = localpath等********/delete [] localpath; //new (LPTSTR)要在对其作其它处理后进行delete//读取整型和写入参考MFC



0 0
原创粉丝点击