MFC对ini文件的操作
来源:互联网 发布:常熟市东张镇淘宝地址 编辑:程序博客网 时间:2024/05/18 12:34
之前讲了MFC对注册表的操作,虽然现在都提倡把信息写入注册表中,但是有时候需要把配置信息写到本地比如工程目录下面,这样便于查看。
API有两种配置文件的函数。
第一种,无路径的,默认读写的配置文件路径C:\Windows\win.ini (前提是没有调用SetRegistryKey函数,如果调用了此函数,就会把配置文件映射到注册表,相应的数据也会写入到注册表而不是配置文件中)
WriteProfileString(_T("appName"), _T("KeyName"), _T("Value"));
第二种,带路径的,只比第一个多一个参数,其余都一样
WritePrivateProfileString(_T("appName"), _T("KeyName"), _T("Value"),_T("D:\\test.ini"));
下面上代码:.h文件
CString GetPrivateSetting(LPCSTR sKey, LPCSTR sDefault);int GetPrivateSetting(LPCSTR sKey, int nDefault);doubleGetPrivateSetting(LPCSTR sKey, double fDefault, LPCSTR sFormat);voidSetPrivateSetting(LPCSTR sKey, LPCSTR sValue);voidSetPrivateSetting(LPCSTR sKey, int nValue);voidSetPrivateSetting(LPCSTR sKey, double fValue, LPCSTR sFormat);.cpp文件
#include "stdafx.h"#include "InitTools.h"//****************************************************************************************************************// Function name : GetPrivateSetting// Description : Get a string from the INI file by sKey.// Created Date : 2015-5-2// Last Updated : 2015-5-2// Author : EasyLiu//****************************************************************************************************************CString GetPrivateSetting(LPCSTR sKey, LPCSTR sDefault){CStringstr;charbuff[100];GetPrivateProfileString("Settings", sKey, sDefault, buff, sizeof(buff), ".\\app.ini");if (strcmp(sDefault, buff) == 0) //If the buff is empty{WritePrivateProfileString("Settings", sKey, sDefault, ".\\app.ini");//write the default value}str = buff;return str;}//****************************************************************************************************************// Function name : GetPrivateSetting// Description : Get a integer number from the INI file by sKey.// Created Date : 2015-5-2// Last Updated : 2015-5-2// Author : EasyLiu//****************************************************************************************************************int GetPrivateSetting(LPCSTR sKey, int nDefault){int n = GetPrivateProfileInt("Settings", sKey, nDefault, ".\\app.ini");if (n == nDefault){CString str;str.Format("%d", nDefault);//只能写入字符串WritePrivateProfileString("Settings", sKey, str, ".\\app.ini");}return n;}//****************************************************************************************************************// Function name : GetPrivateSetting// Description : Get a double precision number from the INI file by sKey.// Created Date : 2015-5-2// Last Updated : 2015-5-2// Author : EasyLiu//****************************************************************************************************************double GetPrivateSetting(LPCSTR sKey, double fDefault, LPCSTR sFormat){CString str;str.Format(sFormat, fDefault);str = GetPrivateSetting(sKey, str);return atof(str);}//****************************************************************************************************************// Function name : SetPrivateSetting// Description : Write a string to the INI file by sKey.// Created Date : 2015-5-2// Last Updated : 2015-5-2// Author : EasyLiu//****************************************************************************************************************void SetPrivateSetting(LPCSTR sKey, LPCSTR sValue){WritePrivateProfileString("Settings", sKey, sValue, ".\\app.ini");}//****************************************************************************************************************// Function name : SetPrivateSetting// Description : Write a integer number to the INI file by sKey.// Created Date : 2015-5-2// Last Updated : 2015-5-2// Author : EasyLiu//****************************************************************************************************************void SetPrivateSetting(LPCSTR sKey, int nValue){CString str;str.Format("%d", nValue);WritePrivateProfileString("Settings", sKey, str, ".\\app.ini");}//****************************************************************************************************************// Function name : SetPrivateSetting// Description : Write a double precision number to the INI file by sKey.// Created Date : 2015-5-2// Last Updated : 2015-5-2// Author : EasyLiu//****************************************************************************************************************void SetPrivateSetting(LPCSTR sKey, double fValue, LPCSTR sFormat){CString str;str.Format(sFormat, fValue);WritePrivateProfileString("Settings", sKey, str, ".\\app.ini");}
这里把配置文件放在了工程目录下面。
0 0
- MFC对ini文件的操作
- MFC中对INI文件的读写操作总结
- VC++2005 MFC INI文件的操作
- MFC 关于ini文件的操作
- mfc 对写ini文件
- 对Ini文件进行操作的类
- C#对*.ini文件的读写操作
- 对ini文件的读写操作
- C#对*.ini文件的读写操作
- Delphi对ini文件的操作
- C#对ini文件的读写操作
- delphi对ini的文件操作
- Delphi对ini文件的操作
- Delphi对ini文件的操作
- VC/MFC 操作INI文件
- VC/MFC 操作ini文件
- MFC操作ini文件方法
- MFC操作ini文件方法
- leetcode-105&106 Construct Binary Tree from Preorder and Inorder Traversal I&II
- [Leetcode]-Pascal's Triangle
- 参数估计、假设检验与回归
- UI_UIImageView 基本操作
- 警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的陷阱
- MFC对ini文件的操作
- 著名的互联网企业的面试笔试真题
- 排序 - 选择法
- LeetCode-Fraction to Recurring Decimal-解题报告
- 获取网络图片
- Matlab插值计算各时刻磁法勘探日变观测值
- 【前端网站】 前端(html、css、js)精品网站收藏
- jquery 回车事件
- c++物件內存布局理解和測試1ddd