当前位置:早雪网网络学院编程文档其他语言 → VC++ 操作 ini 文件

VC++ 操作 ini 文件

减小字体 增大字体 作者:未知  来源:supcode.com收集整理  发布时间:2005-7-1 14:59:33

第一次写文章,不知道怎么上传附件, 只能把 代码贴上去了.

前几天要用到读写ini文件, 自己做了一个 ini 的类, 希望能供大家参考,

各个函数的作用我就不解释了,从名字上就可以看出来.

也希望大家用的时候如果发现错误请指正, 可以发mail 给我,   miaoxf@126.com

// IniFile.h: interface for the CIniFile class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__INCLUDED_)
#define AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CIniFile 
{
public:
 CString GetClassVersion();
 DWORD IniFile_GetKeyNames(CString strSectionName, CStringArray &strArray);

 UINT GetMaxSize_A_Section();
 UINT GetMaxSize_All_SectionNames();

 DWORD IniFile_GetKeyNamesValues(CString strSectionName,CStringArray &strArray);
 BOOL  IniFile_GetStruct(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPVOID lpStruct,UINT uSizeStruct);
 DWORD IniFile_GetSectionNames(CStringArray &strArray );

 BOOL  IniFile_DeleteStruct(LPCTSTR lpSectionName, LPCTSTR lpKeyName);
 BOOL  IniFile_DeleteSection(CString strSectionName);
 BOOL  IniFile_DeleteString(CString strSectionName,CString strKeyName);

 BOOL  IniFile_ModifyString(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPCTSTR lpString);
 BOOL  IniFile_ModifyInt(LPCTSTR lpSectionName, LPCTSTR lpKeyName, int nValue);


 BOOL  IniFile_WriteStruct(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPVOID lpStruct,UINT uSizeStruct);
 BOOL  IniFile_WriteSection(LPCTSTR lpSectionName, LPCTSTR lpString);
 BOOL  IniFile_WriteLongString(LPCTSTR lpValue,LPCTSTR lpSectionName,LPCTSTR lpKeyName,  ...);

 BOOL  IniFile_WriteString(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPCTSTR lpString);
 BOOL  IniFile_WriteInt(LPCTSTR lpSectionName, LPCTSTR lpKeyName, int nValue);

 DWORD IniFile_GetString(CString strSectionName,CString strKeyName,CString strDefault,CString &strReturnedString);
 DWORD IniFile_GetString(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPCTSTR lpDefault,LPTSTR lpReturnedString,DWORD nSize);
 UINT  IniFile_GetInt(LPCTSTR lpSectionName,LPCTSTR lpKeyName,INT nDefault);


 BOOL    EnsureIniFileExist();

 CString GetFilePathName();
 void    SetDefaultFilePathName();
 BOOL    SetFileName(LPCTSTR lpFileName);

 void    SetMaxSize_All_SectionNames(UINT nSize);
 void    SetMaxSize_A_Section(UINT nSize );

 CIniFile();
 virtual ~CIniFile();
 
protected:
 BOOL    IsIniFileExist();
 void    IniFile_InitializeForCreate();

 DWORD   IniFile_GetSectionNames(LPTSTR lpszReturnBuffer, DWORD nSize);
 DWORD   IniFile_GetSection(LPCTSTR lpSectionName,LPTSTR lpReturnedString, DWORD nSize);
 

private:
 CString m_strFileFullName;
 UINT    nMaxSize_All_SectionNames;
 UINT    nMaxSize_A_Section;
};

#endif // !defined(AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__INCLUDED_)

// cpp 文件

// IniFile.cpp: implementation of the CIniFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "IniFile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIniFile::CIniFile()
{
 nMaxSize_All_SectionNames = 1024;
 nMaxSize_A_Section = 1024;
 SetDefaultFilePathName();
}

CIniFile::~CIniFile()
{

}

BOOL CIniFile::IsIniFileExist()
{
 CFile MyFile;
 if (!MyFile.Open(m_strFileFullName,CFile::modeRead))
  return 0;
 MyFile.Close(); 
 return 1;
}

BOOL CIniFile::EnsureIniFileExist()
{
 if (IsIniFileExist())
  return 1;

 CFile MyFile;

 if (!MyFile.Open(m_strFileFullName,CFile::modeCreate))
  return FALSE;

 MyFile.Close(); 

 IniFile_InitializeForCreate();
 
 return TRUE;
}

void CIniFile::SetDefaultFilePathName()
{
 TCHAR exeFullPath[MAX_PATH];
 int len2 = GetModuleFileName(NULL,exeFullPath,MAX_PATH);

 CString strDir(exeFullPath);
 strDir.TrimLeft();
 strDir.TrimRight();

 CString strTemp = strDir.Left(strDir.GetLength() - 3);
 strDir = strTemp;
 strDir += _T("ini");
 m_strFileFullName = strDir;
 return;
}

BOOL CIniFile::IniFile_WriteString(LPCTSTR lpSectionName,
          LPCTSTR lpKeyName,
          LPCTSTR lpString)
{
 return WritePrivateProfileString(lpSectionName,
    lpKeyName,
    lpString,
    m_strFileFullName);
}

BOOL CIniFile::IniFile_WriteInt(LPCTSTR lpSectionName,
       LPCTSTR lpKeyName,
       int nValue)
{
 CString strTemp;
 strTemp.Format(_T("%d"),nValue);
 return WritePrivateProfileString(lpSectionName,
    lpKeyName,
    strTemp,
    m_strFileFullName);
}

UINT CIniFile::IniFile_GetInt(LPCTSTR lpSectionName,
        LPCTSTR lpKeyName,
        INT nDefault)
{
 return GetPrivateProfileInt(lpSectionName,
    lpKeyName,
    nDefault,
    m_strFileFullName);
}

DWORD CIniFile::IniFile_GetString(LPCTSTR lpSectionName,
         LPCTSTR lpKeyName,
         LPCTSTR lpDefault,
         LPTSTR lpReturnedString,
         DWORD nSize)
{
 return GetPrivateProfileString(lpSectionName,
    lpKeyName,
    lpDefault,
    lpReturnedString,
    nSize,
    m_strFileFullName);
}

DWORD CIniFile::IniFile_GetString(CString strSectionName,
         CString strKeyName,
         CString strDefault,
         CString &strReturnedString)
{

 char buf[256];
 DWORD len = GetPrivateProfileString(strSectionName,
    strKeyName,
    strDefault,
    buf,
    256,
    m_strFileFullName);
 buf[len] = 0;

 strReturnedString.Format("%s",buf); 
 return len;
}

CString CIniFile::GetF

[1] [2] [3]  下一页


Tags:VC,++,操作,ini,文件
[数据载入中...] [返回上一页] [打 印]