转载:https://blog.csdn.net/fan380485838/article/details/73188420

在实际项目开发中,会用ini配置文件,在此总结一下对ini读写操作

一:读ini配置文件

DWORD GetPrivateProfileString(
LPCTSTR lpAppName, 
LPCTSTR lpKeyName, 
LPCTSTR lpDefault, 
LPTSTR lpReturnedString, 
DWORD nSize, 
LPCTSTR lpFileName 
);
   其中各参数的意义: 
   lpAppName 根节点name.
lpKeyName 子节点name
   lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量. 
   lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
   nSize : 目的缓存器的大小.
   lpFileName : 是完整的INI文件名.

二:写ini配置文件

BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
  其中各参数的意义:
   LPCTSTR lpAppName 是INI文件中的一个字段名.
   LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名.
   LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
   LPCTSTR lpFileName 是完整的INI文件名.

完整代码:

#include <iostream>
#include <atlstr.h>
using namespace std; void ReadIniFile(LPCWSTR lpFilePath)
{
CString strClass, strName, strAge, strSex, strGrade; //读ini配置文件
::GetPrivateProfileString(_T("StudentInfo"), _T("Class"), _T(""), strClass.GetBuffer(MAX_PATH), MAX_PATH, lpFilePath);
::GetPrivateProfileString(_T("StudentInfo"), _T("Name"), _T(""), strName.GetBuffer(MAX_PATH), MAX_PATH, lpFilePath);
::GetPrivateProfileString(_T("StudentInfo"), _T("Age"), _T(""), strAge.GetBuffer(MAX_PATH), MAX_PATH, lpFilePath);//以字符串形式读取
int age = ::GetPrivateProfileInt(_T("StudentInfo"), _T("Age"), , lpFilePath);//返回值是value
::GetPrivateProfileString(_T("StudentInfo"), _T("Sex"), _T(""), strSex.GetBuffer(MAX_PATH), MAX_PATH, lpFilePath);
::GetPrivateProfileString(_T("StudentInfo"), _T("Grade"), _T(""), strGrade.GetBuffer(MAX_PATH), MAX_PATH, lpFilePath);
} void WriteIniFile(LPCWSTR lpFilePath)
{
CString strClub, strPlayer, strNation, strAge, strGrade;
strClub = "Barcelona";
strPlayer = "Messi";
strNation = "Argentina";
strAge = "";
strGrade = "superstar"; ::WritePrivateProfileString(_T("Team"), _T("Club"), strClub, lpFilePath);
::WritePrivateProfileString(_T("Team"), _T("player"), strPlayer, lpFilePath);
::WritePrivateProfileString(_T("Team"), _T("nationality"), strNation, lpFilePath);
::WritePrivateProfileString(_T("Team"), _T("Age"), strAge, lpFilePath);
::WritePrivateProfileString(_T("Team"), _T("Grade"), strGrade, lpFilePath); } int main()
{ wchar_t szFileFullPath[MAX_PATH];
::GetModuleFileName(NULL, szFileFullPath, MAX_PATH);//获取文件路径
wstring strPath = szFileFullPath;
strPath = strPath.substr(, strPath.rfind('\\') + );
strPath += L"demo.ini"; //写ini配置文件
//WriteIniFile(strPath.c_str()); //读ini配置文件
ReadIniFile(strPath.c_str());
return ;
}

三:配置文件格式:

[StudentInfo]
Class=
Name=tom
Age=
Sex=男
Grade=优秀
[Team]
Club=Barcelona
player=Messi
nationality=Argentina
Age=
Grade=superstar

四:如下需要注意下:
   1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.
   2.文件名的路径中必须为 \\ ,因为在VC++中, \\ 才表示一个 \ .
   3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: ".\\demo.ini".
           4:如果配置文件中有中文字符,需要修改下配置文件的格式。例如将配置文件重新命名为ASCII码格式。

VC++ 实现INI文件读写操作的更多相关文章

  1. C#实现.ini文件读写操作

    1.ini文件是什么?        见百度百科:https://baike.baidu.com/item/ini%E6%96%87%E4%BB%B6/9718973?fr=aladdin 2.C#语 ...

  2. QSettings配置读写-win注册表操作-ini文件读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写     本文地址:http:// ...

  3. Delphi对ini文件的操作

    一.INI文件的结构:; 注释[小节名]关键字=值 INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值. 值的类型有三种:字符串.整型数值和布尔值.其中字符串存贮在INI ...

  4. C#对INI文件读写

    C#本身没有对INI格式文件的操作类,可以自定义一个IniFile类进行INI文件读写. using System; using System.Collections.Generic; using S ...

  5. c语言文件读写操作总结

    C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...

  6. [转]Android - 文件读写操作 总结

     转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...

  7. Delphi ini文件读写

    参考:http://www.cnblogs.com/zhangzhifeng/archive/2011/12/01/2270267.html 一.ini文件的结构 ;这是关于 ini 文件的注释 [节 ...

  8. PHP文件读写操作之文件写入代码

    在PHP网站开发中,存储数据通常有两种方式,一种以文本文件方式存储,比如txt文件,一种是以数据库方式存储,比如Mysql,相对于数据库存储,文件存储并没有什么优势,但是文件读写操作在基本的PHP开发 ...

  9. Java 字节流实现文件读写操作(InputStream-OutputStream)

    Java 字节流实现文件读写操作(InputStream-OutputStream) 备注:字节流比字符流底层,但是效率底下. 字符流地址:http://pengyan5945.iteye.com/b ...

随机推荐

  1. Linux下的awk文本分析命令实例(一)

    1.  入门实例1.1 显示最近登录的5个帐号: [root@localhost ~]# | awk '{print $1}' root root root root reboot 1.2 如果只是显 ...

  2. linux应用以及常用命令

    各目录作用"/" :Linux文件系统的入口.也是最高一级的目录."/bin":基本系统所需要的命令,功能和"/usr/bin"类似,这个目 ...

  3. thinkphp5中使用phpmailer实现发送邮件功能(转载)

    一.开启SMTP服务(使用php发送邮件需要用到SMTP服务,这里以163邮箱的SMTP服务为例). 1.登录163邮箱,在首页上找到“设置”. 2.选择开启的服务,一般都全选,POP3/SMTP/I ...

  4. 8、路由 router

    路由:router 用户功能 /user ----> index.html /user/login ----> login.html /user/reg ----> reg.html ...

  5. React Router API文档

    React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...

  6. Qt自定义界面

    https://blog.csdn.net/zhangxiaoyu_sy/article/details/78925221

  7. tomcat配置内存

    windows: Create a new script named as setenv.bat under TOMCAT_HOME/bin folder holding the following ...

  8. sql server创建windows账户

    --不要干坏事 sql server中使用xp_cmdshell --1.允许配置高级选项 GO RECONFIGURE GO --2.开启xp_cmdshell服务 RECONFIGURE GO - ...

  9. python日期加减法操作

    对日期的一些操作: 对日期的一些操作: 1 #日期转化为字符串并得到指定(或系统日期)n天后的日期--@Eillot 2 def dataTimeToString(dsNow=ReservationT ...

  10. eclipse出现An internal error occurred during: "Building workspace". Java heap space 错误

    出现这个错误,eclipse 会卡死,以及自动退出 解决方案 工程根目录 找到项目中.project文件 删除这两处 第一处: <buildCommand> <name>org ...