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 在我们写的程序当中,总有一 ...
随机推荐
- 微信小程序登录授权并获取手机号
一.请求发送 携带 code 到后台换取 openid var that = this; wx.login({ success(res) { console.log(res); var code = ...
- jenkins插件findbugs+pmd+checkstyle结合sonar与maven(java环境代码质量和代码规范管理)
一.下载jdk并安装(最好jdk官网下载解压安装的) 二.下载maven并安装maven 三.安装jenkins及插件 安装checkstyle.pmd.findbugs.maven.sonar等相关 ...
- saltstack一
Saltstack概述 Salt一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯. salt底层采用动态的连接总线, 使其可以 ...
- 神烦之float
另外一篇文章 : css float 一 历史 Float的设计初衷仅仅是:文字环绕效果(向word中的文字环绕效果) 二 特性 1.包裹性:块级元素如果不设置float,它默认会撑满整个屏幕,而如果 ...
- Error:Failed to resolve: com.android.support:appcompat-v7
repositories { maven { url "https://maven.google.com" } } implementation 'com.android.supp ...
- caffe网络中屏蔽某一层的输出Silence层
屏蔽label输出 layer { name: "silence0" type: "Silence" bottom: "label" pha ...
- 在web-inf外面 使用的是绝对路径进行访问 “/”表示访问文件夹 一层一层方式 我们在windos下访问文件夹也是一层一层的访问
- hdu-2255(带权二分图)
题解:板子题.... #include<iostream> #include<cstring> #include<cstdio> #include<queue ...
- ubuntu系统备份与恢复-remastersys方法
http://blog.csdn.net/yaoqi_isee/article/details/73730010 http://blog.csdn.net/handsome_sheng/article ...
- java代码自动下载Spring Boot用户手册
本示例演示Spring Boot 1.5.9.RELEASE版本的用户手册下载 pom.xml <?xml version="1.0" encoding="UTF- ...