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文件的更多相关文章

  1. vc读取当前路径和读取配置ini文件

    //获取路径 std::string GetApplicationDir() { HMODULE hModule = GetModuleHandleW(NULL); WCHAR wpath[MAX_P ...

  2. C#中读写配置参数文件(利用Windows的API)

      读配置文件与写配置文件的核心代码如下:   [DllImport("kernel32")] // 读配置文件方法的6个参数:所在的分区(section).键值. 初始缺省值. ...

  3. C#配置.INI文件

    百度搜了一些资料,好多没给示例,只给了代码.让人看了直接懵逼,后来找了个靠谱的:http://www.jb51.net/article/118591.htm

  4. INI文件的读写

    public class INIoperation { string inipath = Utils.GetMapPath("/Integration/Sync/set.ini") ...

  5. 绿色安装MySQL5.7版本----配置my.ini文件注意事项

    前言 由于前段时间电脑重装,虽然很多软件不在C盘,但是由于很多注册表以及关联文件被删除,很多软件还需要重新配置甚至卸载重装. 使用MySQL时就遇到了这种情况,在修改配置文件无效的情况下选择了重新安装 ...

  6. Delphi操作Ini文件

    Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件       关键字1=值1       关键子2=值2INI文件允许有多个小节, ...

  7. delphi读取ini文件

    ini文件在系统配置及应用程序参数保存与设置方面,具有很重要的作用,所以可视化的编程一族,如vb.vc.vfp.delphi等都提供了读写ini文件的方法,其中delphi中操作ini文件,最为简洁, ...

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

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

  9. VB读写INI文件的四个函数以及相关API详细说明

    WritePrivateProfileString函数说明  来源:http://blog.csdn.net/wjb9921/article/details/2005000 在我们写的程序当中,总有一 ...

随机推荐

  1. VUE项目问题之:去掉url中的#/

    一.问题 使用VUE路由,项目的url总是带有锚点,如下: http://localhost:8082/#/ 二.解决 修改路由文件中 index.js 文件,即 src --> router ...

  2. SWT 几个sample网站

    https://www.programcreek.com/java-api-examples/org.eclipse.swt.custom.ScrolledComposite https://o7pl ...

  3. freemarker 设置中文

    在web中添加一段代码 <servlet> <servlet-name>freemarker</servlet-name> <servlet-class> ...

  4. HTTP协议 - 基础认识

    在http协议使用场景上我们最熟悉的可能就是浏览器了,作为本系列第一篇,就讲一个问题  ”浏览器怎么连接上服务器并获取网页内容的“ : 首先 浏览器怎么连接上服务器的? 如果对OSI七层模型或者TCP ...

  5. js 中常用的循环

    参考文章: 1.js几种数组遍历方式以及性能分析对比 2.javaScript遍历对象.数组总结 首先是数组中可以使用的 1.for 循环 for (let i = 0; i < xxx.len ...

  6. hdu 4578 Transformation 线段树多种操作裸题

    自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...

  7. 简单介绍一下在CentOS上安装Docker。

    简单介绍一下在CentOS上安装Docker. 前置条件: 64-bit 系统 kernel 3.10+ 1.检查内核版本,返回的值大于3.10即可. $ uname -r 2.使用 sudo 或 r ...

  8. HTML5-Video视频-基础篇

    展示视频 视频 <video width=" controls="controls"> <source src="movie.mp4" ...

  9. [代码]--给任意网站添加聊天功能,随时聊(fa)天(che)

    感谢“topurl.cn”制作此功能并分享. 这是一段代码,在打开的网页中使用,可以加载一个外挂形式的聊天室功能, 就可以和同样访问此网站进行相同操作的网友进行聊(fa)天(che)了. 使用方法: ...

  10. 【XSY2701】异或图 线性基 容斥原理

    题目描述 定义两个图\(G_1\)与\(G_2\)的异或图为一个图\(G\),其中图\(G\)的每条边在\(G_1\)与\(G_2\)中出现次数和为\(1\). 给你\(m\)个图,问你这\(m\)个 ...