MFC中使用函数实现ini文件的连续读写

实现的思路:

首先通过读取文件中的count值,确定当前信息条数;

第二步:将count进行累加,把信息写到累加后的键值"="的后面;

第三步:写入count累加值,实现连续读写;

第四步:写入需要保存的信息;

第五步:读取当前count值;

第六步:根据当前count值,读取写入的信息;

最后就是通过MessageBox来验证写入的信息。

//读取Ini文件中的Count值

GetPrivateProfileString(L"FileName", L"Count", NULL, wcharBuf, MAX_PATH, L"C:\\Users\\source\\repos\\FileNameInfo.ini");

//读到后累加一,wchar_t转int再转CString,实现连续读写

CountNumb = _ttol(wcharBuf) + 1;

StrCountNum.Format(_T("%d"), CountNumb);

//合成Result+number(键值)

KeyName = StrKey + StrCountNum;

//写入count累加值

::WritePrivateProfileString(L"FileName", L"Count", StrCountNum, L"C:\\Users\\source\\repos\\FileNameInfo.ini");

//写入结果strTempEditPath

::WritePrivateProfileString(L"FileName", KeyName, strTempEditPath, L"C:\\Users\\source\\repos\\FileNameInfo.ini");

//获取当前count值

GetPrivateProfileString(L"FileName", L"Count", NULL, wcharBuf, MAX_PATH, L"C:\\Users\\source\\repos\\FileNameInfo.ini");

//获取当前Result+number(键值)后的数据

GetPrivateProfileString(L"FileName", KeyName, NULL, wcharBuf, MAX_PATH, L"C:\\Users\\source\\repos\\FileNameInfo.ini");

//将读取到的数据存到CString 变量中

strSearch = wcharBuf;

//从右边开始截取

strFileName = strSearch.Right(strSearch.GetLength()-39);

//MessageBox(strFileName);//验证信息

相关推荐
一拳一个呆瓜3 天前
【MFC】对话框属性:Absolute Align(绝对对齐)
c++·mfc
深耕AI3 天前
MFC 图形设备接口详解:小白从入门到掌握
c++·mfc
ChindongX3 天前
CMap常用函数
mfc
一拳一个呆瓜4 天前
【MFC】对话框属性:X Pos(X位置),Y Pos(Y位置)
c++·mfc
一拳一个呆瓜4 天前
【MFC】对话框属性:Center(居中)
c++·mfc
一拳一个呆瓜4 天前
【MFC】对话框:位置属性(居中、绝对对齐、X位置Y位置)应用示例
c++·mfc
ChindongX5 天前
CString(MFC/ATL 框架)和 QString(Qt 框架)
qt·mfc
深耕AI5 天前
【MFC视图和窗口基础:文档/视图的“双胞胎”魔法 + 单文档程序】
c++·mfc
ChindongX5 天前
CStringArray 和 CStringList
mfc
深耕AI5 天前
【MFC典型类和函数:CString的字符串魔法与Afx全局函数的便利店】
c++·mfc