[转]MFC读写读写读写读写ini配置文件配置文件配置文件配置文件 -----Email:liping_xiong@163.com
2013-04-06 16:50
513 查看
新建头文件如:ini.h,在编辑区域粘贴以下内容:
/////////////////////////////////////////////////////////////////////////
// ini.h: interface for the Cini class.
#if !defined(AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)
#define AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxwin.h>
class Cini
{
public:
static DWORD ReadString (char *section, char * key, char stringtoread[], char * filename);
static BOOL WriteString(LPCTSTR section, LPCTSTR key,char* stringtoadd, char *filename);
Cini();
virtual ~Cini();
};
#endif // !defined(AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)
新建资源文件如:ini.cpp,在编辑区域黏贴以下内容:
/////////////////////////////////////////////////////////////////////////
// ini.cpp: implementation of the Cini class.
#include "stdafx.h"
#include "ini.h"
/////////////////////////////////////////////////////////////////////////
//Cini类的构造函数和析构函数
Cini::Cini()
{
}
Cini::~Cini()
{
}
/////////////////////////////////////////////////////////////////////////
//写字符串到INI文件,GetLastError()函数用于返回写入失败
void error(LPSTR lpszFunction)
{
CHAR szBuf[80];
DWORD dw = GetLastError();
sprintf(szBuf, "%s failed: GetLastError returned %u\n",lpszFunction, dw);
MessageBox(NULL, szBuf, "Error", MB_OK);
ExitProcess(dw);
}
BOOL Cini::WriteString(LPCTSTR section, LPCTSTR key, char *stringtoadd, char *filename)
{
CHAR FilePath[255];
GetModuleFileName(NULL,FilePath,255);
(strrchr(FilePath,'\\'))[1] = 0;
strcat(FilePath,filename);
return ::WritePrivateProfileString(section,key,stringtoadd,FilePath);
}
/////////////////////////////////////////////////////////////////////////
//从INI文件中读取字符串
DWORD Cini::ReadString(char *section, char * key, char stringtoread[], char * filename)
{
CHAR FilePath[255];
GetModuleFileName(NULL,FilePath,255);
(strrchr(FilePath,'\\'))[1] = 0;
strcat(FilePath,filename);
return ::GetPrivateProfileString(section, key,NULL,stringtoread,255,FilePath);
}
当程序加载的时候读配置文件:
首先要在应用程序文件头部引用ini.h头文件
如下:
#include "ini.h"
找到OnInitDialog()函数,在其下黏贴如下代码:
// 保存ini各项值数组
char szOption1[MAX_PATH];
char szOption2[MAX_PATH];
char szOption3[MAX_PATH];
// 读取ini各项值
Cini::ReadString("配置信息", "选项1", szOption1, "test.ini");
Cini::ReadString("配置信息", "选项2", szOption2, "test.ini");
Cini::ReadString("配置信息", "选项3", szOption3, "test.ini");
m_strEdit1.Format("%s",szOption1);
m_strEdit2.Format("%s",szOption2);
m_strEdit3.Format("%s",szOption3);
UpdateData(FALSE);
说明:m_strEdit1,m_strEdit2,m_strEdit2是三个文本框对象的实例
当卸载(关闭)程序的时候写配置文件;
找到DestroyWindow()函数,在其下黏贴如下代码:
UpdateData(TRUE);
CString str1 = m_strEdit1;
CString str2 = m_strEdit2;
CString str3 = m_strEdit3;
char *p1 = str1.GetBuffer(str1.GetLength()+1);
char *p2 = str2.GetBuffer(str2.GetLength()+1);
char *p3 = str3.GetBuffer(str3.GetLength()+1);
Cini::WriteString("配置信息", "选项1", p1, "test.ini");
Cini::WriteString("配置信息", "选项2", p2, "test.ini");
Cini::WriteString("配置信息", "选项3", p3, "test.ini");
说明:m_strEdit1,m_strEdit2,m_strEdit2是三个文本框对象的实例
/////////////////////////////////////////////////////////////////////////
// ini.h: interface for the Cini class.
#if !defined(AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)
#define AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxwin.h>
class Cini
{
public:
static DWORD ReadString (char *section, char * key, char stringtoread[], char * filename);
static BOOL WriteString(LPCTSTR section, LPCTSTR key,char* stringtoadd, char *filename);
Cini();
virtual ~Cini();
};
#endif // !defined(AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)
新建资源文件如:ini.cpp,在编辑区域黏贴以下内容:
/////////////////////////////////////////////////////////////////////////
// ini.cpp: implementation of the Cini class.
#include "stdafx.h"
#include "ini.h"
/////////////////////////////////////////////////////////////////////////
//Cini类的构造函数和析构函数
Cini::Cini()
{
}
Cini::~Cini()
{
}
/////////////////////////////////////////////////////////////////////////
//写字符串到INI文件,GetLastError()函数用于返回写入失败
void error(LPSTR lpszFunction)
{
CHAR szBuf[80];
DWORD dw = GetLastError();
sprintf(szBuf, "%s failed: GetLastError returned %u\n",lpszFunction, dw);
MessageBox(NULL, szBuf, "Error", MB_OK);
ExitProcess(dw);
}
BOOL Cini::WriteString(LPCTSTR section, LPCTSTR key, char *stringtoadd, char *filename)
{
CHAR FilePath[255];
GetModuleFileName(NULL,FilePath,255);
(strrchr(FilePath,'\\'))[1] = 0;
strcat(FilePath,filename);
return ::WritePrivateProfileString(section,key,stringtoadd,FilePath);
}
/////////////////////////////////////////////////////////////////////////
//从INI文件中读取字符串
DWORD Cini::ReadString(char *section, char * key, char stringtoread[], char * filename)
{
CHAR FilePath[255];
GetModuleFileName(NULL,FilePath,255);
(strrchr(FilePath,'\\'))[1] = 0;
strcat(FilePath,filename);
return ::GetPrivateProfileString(section, key,NULL,stringtoread,255,FilePath);
}
当程序加载的时候读配置文件:
首先要在应用程序文件头部引用ini.h头文件
如下:
#include "ini.h"
找到OnInitDialog()函数,在其下黏贴如下代码:
// 保存ini各项值数组
char szOption1[MAX_PATH];
char szOption2[MAX_PATH];
char szOption3[MAX_PATH];
// 读取ini各项值
Cini::ReadString("配置信息", "选项1", szOption1, "test.ini");
Cini::ReadString("配置信息", "选项2", szOption2, "test.ini");
Cini::ReadString("配置信息", "选项3", szOption3, "test.ini");
m_strEdit1.Format("%s",szOption1);
m_strEdit2.Format("%s",szOption2);
m_strEdit3.Format("%s",szOption3);
UpdateData(FALSE);
说明:m_strEdit1,m_strEdit2,m_strEdit2是三个文本框对象的实例
当卸载(关闭)程序的时候写配置文件;
找到DestroyWindow()函数,在其下黏贴如下代码:
UpdateData(TRUE);
CString str1 = m_strEdit1;
CString str2 = m_strEdit2;
CString str3 = m_strEdit3;
char *p1 = str1.GetBuffer(str1.GetLength()+1);
char *p2 = str2.GetBuffer(str2.GetLength()+1);
char *p3 = str3.GetBuffer(str3.GetLength()+1);
Cini::WriteString("配置信息", "选项1", p1, "test.ini");
Cini::WriteString("配置信息", "选项2", p2, "test.ini");
Cini::WriteString("配置信息", "选项3", p3, "test.ini");
说明:m_strEdit1,m_strEdit2,m_strEdit2是三个文本框对象的实例
相关文章推荐
- C++中通过MFC对ini初始化,配置文件的读写使用
- MFC 读写配置文件 ini
- WinCE下配置(ini)文件读写,基于MFC的
- MFC读写INI配置文件
- MFC创建INI配置文件并读写INI配置文件
- MFC读写INI配置文件
- MFC 读写配置文件——ini文件
- MFC读写ini配置文件(WritePrivateProfileString,GetPrivateProfileString,GetPrivateProfileInt)
- MFC学习笔记——读写配置文件(.ini)和注册表
- 用C#读写ini配置文件
- 在VC中读写ini配置文件的函数
- C#中读写INI配置文件
- C++针对ini配置文件读写大全
- C语言ini格式配置文件的读写
- python3 如何读写配置文件(ini)
- [C/C++]伪读写ini配置文件
- WritePrivateProfileString等读写.ini配置文件
- 用C#读写ini配置文件
- C#读写ini配置文件
- 学习笔记之一读写配置文件(ini)