MFC读写配置ini文件
https://blog.csdn.net/naibozhuan3744/article/details/78783446
https://blog.csdn.net/rayborn1105/article/details/8192142
https://blog.csdn.net/g5dsk/article/details/6860162
// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <windows.h> //using namespace std; int main()
{ //std::string strName, strTemp;
//int nAge;
//strName = "张三";
//nAge = 12;
//::WritePrivateProfileString(TEXT("StudentInfo"), TEXT("Name"), TEXT("张三"), TEXT(".\\student.ini")); // write
WritePrivateProfileString(TEXT("LiMing"), TEXT("Sex"), TEXT("Man"), TEXT(".\\student.ini"));// 路径realse
WritePrivateProfileString(TEXT("LiMing"), TEXT("Age"), TEXT(""), TEXT(".\\student.ini"));
WritePrivateProfileString(TEXT("Fangfang"), TEXT("Sex"), TEXT("Woman"), TEXT(".\\student.ini"));
WritePrivateProfileString(TEXT("Fangfang"), TEXT("Age"), TEXT(""), TEXT(".\\student.ini")); // read LPTSTR LiMingSex = new TCHAR[] ;
int LiMingAge = ;
LPTSTR FangfangSex = new TCHAR[];;
int FangfangAge = ;
GetPrivateProfileString(TEXT("LiMing"), TEXT("Sex"), TEXT(""), LiMingSex, ,TEXT(".\\student.ini"));
LiMingAge = GetPrivateProfileInt(TEXT("LiMing"), TEXT("Age"), , TEXT(".\\student.ini")); char LM[] = {};
strncpy_s(LM, (char *)LiMingSex,); system("pause");
return ;
}
[Section1] Key11=value11 Key12=value12 [Section2] Key21=value21 Key22=value22 ... [SectionN] KeyN1=valueN1 KeyN2=valueN2
(1)操作系统配置文件Win.ini的函数:
|
函数名 |
功能 |
|
GetProfileSection |
读取win.ini中指定节lpAppName中所有键名及其值。lpReturnedString字符串形式如下: Key1=Value1/0Key2=Value2/0…KeyN=ValueN/0/0 |
|
GetProfileString |
读取win.ini中指定节lpAppName中键名为lpKeyName对应变量的字符串值。 |
|
GetProfileInt |
读取win.ini中指定节lpAppName中键名为lpKeyName对应变量的整数值。 |
|
WriteProfileSection |
写(替换)win.ini中指定节lpAppName中的键值。 lpString字符串形式同GetProfileSection中的lpReturnedString。 |
|
WriteProfileString |
写(替换)win.ini中指定节lpAppName中键名为lpKeyName对应变量的字符串值。 |
(2)操作用户自定义配置文件(PrivateProfile.ini)的函数:
|
函数名 |
功能 |
|
GetPrivateProfileSectionNames |
读取lpFileName指定的配置文件中所有的节名。lpszReturnBuffer字符串形式如下: Section1/0Section2/0…SectionN/0/0 |
|
GetPrivateProfileSection |
同GetProfileSection。 |
|
GetPrivateProfileString |
同GetProfileString。 |
|
GetPrivateProfileInt |
同GetProfileInt |
|
GetPrivateProfileStruct |
须同WritePrivateProfileStruct配套使用。 |
|
WritePrivateProfileSection |
同WriteProfileSection |
|
WritePrivateProfileString |
同WriteProfileString |
|
WritePrivateProfileStruct |
不常用。 |
用MFC配置ini文件的核心函数有三个,分别为WritePrivateProfileString和GetPrivateProfileString和GetPrivateProfileInt。下面分别讲解这三个核心函数的功能和各个参数的意义。
WritePrivateProfileString函数
BOOL WritePrivateProfileString(
LPCTSTRlpAppName,
LPCTSTRlpKeyName,
LPCTSTRlpString,
LPCTSTRlpFileName
);
//其中各参数的意义:
LPCTSTR lpAppName; //是INI文件中的一个字段名.
LPCTSTR lpKeyName;//是lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString; //是键值, 也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName;//是完整的INI文件名.
WritePrivateProfileString("StudentInfo1","身份证","",".\\ConfigFile\\ConfigInit.ini");
[StudentInfo] Name = 李四 Age = 身份证 = [StudentInfo1] Name = 李四 Age = 身份证 =
GetPrivateProfileString函数
DWORD GetPrivateProfileString(
LPCTSTRlpAppName,
LPCTSTRlpKeyName,
LPCTSTRlpDefault,
LPTSTRlpReturnedString,
DWORDnSize,
LPCTSTRlpFileName
);
//其中各参数的意义:
//前二个参数与 WritePrivateProfileString中的意义一样.
lpAppName; //是INI文件中的一个字段名.
lpKeyName;// 是lpAppName下的一个键名,通俗讲就是变量名.
lpDefault;// : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString;// : 接收INI文件中的值的CString对象,即目的缓存器.
nSize;// : 目的缓存器的大小.
lpFileName;// : 是完整的INI文件名.
GetPrivateProfileInt函数
UINT GetPrivateProfileInt(
LPCTSTRlpAppName,
LPCTSTRlpKeyName,
INTnDefault,
LPCTSTRlpFileName
);//其中各参数的意义:
//前二个参数与 WritePrivateProfileString中的意义一样.
LPCTSTR lpAppName; //是INI文件中的一个字段名.
LPCTSTR lpKeyName;//是lpAppName下的一个键名,通俗讲就是变量名.
INT nDefault; //如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
LPCTSTR lpFileName;//是完整的INI文件名.
调用
GetPrivateProfileInt("StudentInfo2","Age", ,".\\ConfigFile\\ConfigInit.ini");
#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>
#pragma comment(lib, "strsafe.lib") #define INI_FILE_NAME _T("ini_test.ini") TCHAR gIniFileFullPath[MAX_PATH] = {_T('\0')}; int main()
{
DWORD dwRet = ;
BOOL bRet = FALSE; // 获取 ini 文件全路径
dwRet = ::GetModuleFileName(NULL, gIniFileFullPath, MAX_PATH);
if (0UL == dwRet)
{
_ftprintf(stderr, _T("Error: Error occurs in calling GetModuleFileName, ")
_T("error code is %lu.\n"),
::GetLastError());
return -;
}
if (MAX_PATH == dwRet && ERROR_INSUFFICIENT_BUFFER == ::GetLastError())
{
_ftprintf(stderr, _T("Error: The buffer is too small to hold the module name.\n"));
return -;
} _tprintf(_T("The full path for the current module is: \n\t%s\n"), gIniFileFullPath); DWORD dwLoopIdx = dwRet - ;
while (gIniFileFullPath[dwLoopIdx] != _T('\\'))
{
--dwLoopIdx;
}
::StringCchCopy(gIniFileFullPath + (dwLoopIdx + ), MAX_PATH - (dwLoopIdx + ), INI_FILE_NAME);
_tprintf(_T("The full path for %s is: \n\t%s\n"), INI_FILE_NAME, gIniFileFullPath); // ---------------------------------------------- WritePrivateProfileSection
// 注: 如果 gIniFileFullPath 表示的 .ini 文件不存在, 会先创建一个.
TCHAR szInsertedKeyValuePair[] = {_T('\0')}; #if defined(_UNICODE) || defined(UNICODE)
wchar_t* dest = NULL;
wchar_t* src = NULL;
size_t count = ; dest = szInsertedKeyValuePair;
src = L"Key11=defaultValue11";
count = wcslen(L"Key11=defaultValue11");
wmemcpy(dest, src, count); dest += count;
src = L"\0";
count = ;
wmemcpy(dest, src, count); dest += count;
src = L"Key12=defaultValue12";
count = wcslen(L"Key12=defaultValue12");
wmemcpy(dest, src, count); dest += count;
src = L"\0";
count = ;
wmemcpy(dest, src, count); dest += count;
src = L"Key13=defaultValue13";
count = wcslen(L"Key13=defaultValue13");
wmemcpy(dest, src, count); dest += count;
src = L"\0";
count = ;
wmemcpy(dest, src, count);
#else
char* dest = NULL;
char* src = NULL;
size_t count = ; dest = szInsertedKeyValuePair;
src = "Key11=defaultValue11";
count = strlen("Key11=defaultValue11");
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "\0";
count = ;
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "Key12=defaultValue12";
count = strlen("Key12=defaultValue12");
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "\0";
count = ;
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "Key13=defaultValue13";
count = strlen("Key13=defaultValue13");
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "\0";
count = ;
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count);
#endif // defined(_UNICODE) || defined(UNICODE) bRet = ::WritePrivateProfileSection(_T("Section1"), szInsertedKeyValuePair, gIniFileFullPath);
if (FALSE == bRet)
{
_ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileSection, ")
_T("error code is %lu.\n"),
::GetLastError());
return -;
}
// ---------------------------------------------- WritePrivateProfileSection // ----------------------------------------------- WritePrivateProfileString
TCHAR szStr4Int[] = {_T('\0')};
_itot(-, szStr4Int, );
bRet = ::WritePrivateProfileString(_T("Section1"), _T("Key12"), szStr4Int, gIniFileFullPath);
if (FALSE == bRet)
{
_ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileString, ")
_T("error code is %lu.\n"),
::GetLastError());
return -;
} bRet = ::WritePrivateProfileString(_T("Section1"), _T("Key13"), _T("Value13"), gIniFileFullPath);
if (FALSE == bRet)
{
_ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileString, ")
_T("error code is %lu.\n"),
::GetLastError());
return -;
}
// ----------------------------------------------- WritePrivateProfileString // ------------------------------------------- GetPrivateProfileSectionNames
TCHAR szReturnBuffer[] = {_T('\0')};
dwRet = ::GetPrivateProfileSectionNames(szReturnBuffer, , gIniFileFullPath);
if ( - == dwRet)
{
_ftprintf(stderr, _T("Error: szReturnBuffer is not large enough to ")
_T("contain all the section names associated with ")
_T("the specified initialization file.\n"));
return -;
}
// ------------------------------------------- GetPrivateProfileSectionNames // ------------------------------------------------ GetPrivateProfileSection
// 在 Section1 末尾追加 Key14=defaultValue14
dwRet = ::GetPrivateProfileSection(_T("Section1"), szReturnBuffer, , gIniFileFullPath);
if ( - == dwRet)
{
_ftprintf(stderr, _T("Error: szReturnBuffer is not large enough to ")
_T("contain all the key name and value pairs ")
_T("associated with Section1.\n"));
return -;
} #if defined(_UNICODE) || defined(UNICODE)
dest = szReturnBuffer + dwRet;
src = L"Key14=DefaultValue14";
count = wcslen(L"Key14=DefaultValue14");
wmemcpy(dest, src, count); dest += count;
src = L"\0";
count = ;
wmemcpy(dest, src, count);
#else
dest = szReturnBuffer + dwRet;
src = "Key14=DefaultValue14";
count = strlen("Key14=DefaultValue14");
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "\0";
count = ;
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count);
#endif // defined(_UNICODE) || defined(UNICODE) bRet = ::WritePrivateProfileSection(_T("Section1"), szReturnBuffer, gIniFileFullPath);
if (FALSE == bRet)
{
_ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileSection, ")
_T("error code is %lu.\n"),
::GetLastError());
return -;
}
// ------------------------------------------------ GetPrivateProfileSection // ---------------------------------------------- WritePrivateProfileSection
// 在 .ini 文件末尾追加 Section2
ZeroMemory(static_cast<void*>(szInsertedKeyValuePair), sizeof(szInsertedKeyValuePair)); #if defined(_UNICODE) || defined(UNICODE)
dest = szInsertedKeyValuePair;
src = L"Key21=defaultValue21";
count = wcslen(L"Key21=defaultValue21");
wmemcpy(dest, src, count); dest += count;
src = L"\0";
count = ;
wmemcpy(dest, src, count); dest += count;
src = L"Key22=defaultValue22";
count = wcslen(L"Key22=defaultValue22");
wmemcpy(dest, src, count); dest += count;
src = L"\0";
count = ;
wmemcpy(dest, src, count);
#else
dest = szInsertedKeyValuePair;
src = "Key21=defaultValue21";
count = strlen("Key21=defaultValue21");
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "\0";
count = ;
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "Key22=defaultValue22";
count = strlen("Key22=defaultValue22");
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count;
src = "\0";
count = ;
memcpy(static_cast<void*>(dest), static_cast<void*>(src), count);
#endif // defined(_UNICODE) || defined(UNICODE) bRet = ::WritePrivateProfileSection(_T("Section2"), szInsertedKeyValuePair, gIniFileFullPath);
if (FALSE == bRet)
{
_ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileSection, ")
_T("error code is %lu.\n"),
::GetLastError());
return -;
}
// ---------------------------------------------- WritePrivateProfileSection // ---------------------------------------------------- GetPrivateProfileInt
// 注: MSDN 中, 该函数原型的返回值数据类型为 UINT, 但, 并非是说它只能返回
// 无符号整型值. 通过该函数, .ini 文件中的负数是可以正确取得的.
int nIntValue4Key12 = ::GetPrivateProfileInt(_T("Section1"), _T("Key12"), -, gIniFileFullPath);
_tprintf(_T("\nnIntValue4Key12 = %i\n"), nIntValue4Key12);
// ---------------------------------------------------- GetPrivateProfileInt // ------------------------------------------------- GetPrivateProfileString
// Read value for Key11
dwRet = ::GetPrivateProfileString(_T("Section1"),
_T("Key11"),
_T("Key11 key cannot be found in the initialization file."),
szReturnBuffer,
MAX_PATH,
gIniFileFullPath);
if (MAX_PATH - == dwRet)
{
_ftprintf(stderr, _T("Error: Neither lpAppName nor lpKeyName is NULL ")
_T("and the supplied destination buffer is too small ")
_T("to hold the requested string.\n"));
return -;
}
if (MAX_PATH - == dwRet)
{
_ftprintf(stderr, _T("Error: Either lpAppName or lpKeyName is NULL and ")
_T("the supplied destination buffer is too small to ")
_T("hold all the strings.\n"));
return -;
}
_tprintf(_T("\nValue for Key11 is %s.\n"), szReturnBuffer); // Read value for Key12
dwRet = ::GetPrivateProfileString(_T("Section1"),
_T("Key12"),
_T("Key12 key cannot be found in the initialization file."),
szReturnBuffer,
MAX_PATH,
gIniFileFullPath);
if (MAX_PATH - == dwRet)
{
_ftprintf(stderr, _T("Error: Neither lpAppName nor lpKeyName is NULL ")
_T("and the supplied destination buffer is too small ")
_T("to hold the requested string.\n"));
return -;
}
if (MAX_PATH - == dwRet)
{
_ftprintf(stderr, _T("Error: Either lpAppName or lpKeyName is NULL and ")
_T("the supplied destination buffer is too small to ")
_T("hold all the strings.\n"));
return -;
}
_tprintf(_T("Value for Key12 is %s.\n"), szReturnBuffer); // Read value for Key13
dwRet = ::GetPrivateProfileString(_T("Section1"),
_T("Key13"),
_T("Key13 key cannot be found in the initialization file."),
szReturnBuffer,
MAX_PATH,
gIniFileFullPath);
if (MAX_PATH - == dwRet)
{
_ftprintf(stderr, _T("Error: Neither lpAppName nor lpKeyName is NULL ")
_T("and the supplied destination buffer is too small ")
_T("to hold the requested string.\n"));
return -;
}
if (MAX_PATH - == dwRet)
{
_ftprintf(stderr, _T("Error: Either lpAppName or lpKeyName is NULL and ")
_T("the supplied destination buffer is too small to ")
_T("hold all the strings.\n"));
return -;
}
_tprintf(_T("Value for Key13 is %s.\n\n"), szReturnBuffer);
// ------------------------------------------------- GetPrivateProfileString return ;
MFC读写配置ini文件的更多相关文章
- vc读取当前路径和读取配置ini文件
//获取路径 std::string GetApplicationDir() { HMODULE hModule = GetModuleHandleW(NULL); WCHAR wpath[MAX_P ...
- C#中读写配置参数文件(利用Windows的API)
读配置文件与写配置文件的核心代码如下: [DllImport("kernel32")] // 读配置文件方法的6个参数:所在的分区(section).键值. 初始缺省值. ...
- C#配置.INI文件
百度搜了一些资料,好多没给示例,只给了代码.让人看了直接懵逼,后来找了个靠谱的:http://www.jb51.net/article/118591.htm
- INI文件的读写
public class INIoperation { string inipath = Utils.GetMapPath("/Integration/Sync/set.ini") ...
- 绿色安装MySQL5.7版本----配置my.ini文件注意事项
前言 由于前段时间电脑重装,虽然很多软件不在C盘,但是由于很多注册表以及关联文件被删除,很多软件还需要重新配置甚至卸载重装. 使用MySQL时就遇到了这种情况,在修改配置文件无效的情况下选择了重新安装 ...
- Delphi操作Ini文件
Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件 关键字1=值1 关键子2=值2INI文件允许有多个小节, ...
- delphi读取ini文件
ini文件在系统配置及应用程序参数保存与设置方面,具有很重要的作用,所以可视化的编程一族,如vb.vc.vfp.delphi等都提供了读写ini文件的方法,其中delphi中操作ini文件,最为简洁, ...
- QSettings配置读写-win注册表操作-ini文件读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写 本文地址:http:// ...
- VB读写INI文件的四个函数以及相关API详细说明
WritePrivateProfileString函数说明 来源:http://blog.csdn.net/wjb9921/article/details/2005000 在我们写的程序当中,总有一 ...
随机推荐
- 简单JQuery+AJAX+Servlet的计算器实现
index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...
- WPF如何实现TreeView节点重命名
我们经常看到一些软件比如酷狗音乐,在对列表右键进行重命名的时候,当前列表会泛白并且进入可编辑状态,当我们更改完成后就会并进入非编辑状态,这些具体是怎么实现的呢?下面的方法也许会提供一些思路,下面的Tr ...
- 如何抓取电商的数据 & Python
如何抓取电商的数据 & Python https://www.zhihu.com/question/40720286 https://www.zhihu.com/question/382455 ...
- 手机连接WiFi有感叹号x怎么回事?如何消除手机WiFi感叹号?
经过多年的革新,现在的安卓系统已经非常优秀了,某些程度已经超越iOS,卡顿和耗电也不再是安卓系统的代名词了.而为了体验到最优秀的安卓系统,不少人都会购买海外的手机,因为海外手机的安卓系统都比较精简,非 ...
- QXcbConnection: Could not connect to display
import matplotlib; matplotlib.use('agg') 注意:要添加到所有matplotlib前面,否则不起作用
- winserver 2008 R2服务器安装IIS
winserver 2008 R2 IIS7 安装IIS 打开服务器管理器 选择“角色”,右击添加角色 点击“下一步” 勾选”Web服务器(IIS)“,点击”下一步“ 勾选”常见Http功能.应用程序 ...
- kubernetes ceph-rbd挂载步骤 类型storageClass
由于kubelet本身并不支持rbd的命令,所以需要添加一个kube系统插件: 下载插件 quay.io/external_storage/rbd-provisioner 下载地址: https:// ...
- 查询SQL执行情况
/* 查询SQL执行情况 包含逻辑读取信息,执行信息等情况*/ SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total ...
- Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖
题意:图没什么用 给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖 最小路径覆盖=|G|-最大匹配数 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...