zoukankan      html  css  js  c++  java
  • [转]MFC读写读写读写读写ini配置文件配置文件配置文件配置文件 Email:liping_xiong@163.com

    1. 新建头文件如: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_)

     

    1. 新建资源文件如: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);

    }

     

    1. 当程序加载的时候读配置文件

    首先要在应用程序文件头部引用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是三个文本框对象的实例

     

    1. 当卸载(关闭)程序的时候写配置文件

    找到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是三个文本框对象的实例

     

  • 相关阅读:
    实验5&期中考试后两题
    实验四——再探类
    实验3—初识类
    10.29算法训练——poj1475双重BFS
    10.25算法训练——裸线段树
    探索邻接表
    Hadoop学习之Combiner
    关于VLM模式下linux压缩根目录空间的心得
    C#微信公众号开发系列教程四(接收普通消息)
    c#分部类型详解
  • 原文地址:https://www.cnblogs.com/blueswitkey/p/3002626.html
Copyright © 2011-2022 走看看