//Inifile.h
/*****************************************************************文件名:读取当前目录下CIniFile类文件
日期:2004-1-18 作者:chao_jie E_Mail:chaojie2003@yahoo.com.cn功能:系统提供的WriteProfileInt()等函数,写到注册表或
系统ini文件里,调用比较麻烦,而且会占用注册表资 源,下载了网上的ini类用起来又不太顺手,所以参照 写了一个读取当前目录下ini文件的类。程序有详细的 注释,欢迎批评指正。*****************************************************************/
/*//
使用方法: 1.先把IniFile.h,IniFile.ccp加入Porject,在要使用该类的头文件中加入 #include "IniFile.h",及CIniFile IniFile;2.读取和写入:
string = IniFile.GetString("section","item","默认"); int = IniFile.GetInt("section","item",666);IniFile.SetString("section","item","设置值");
IniFile.SetInt("section","item",666);3.ini文件名及路径在初始化时,用SetFileName()设置
注意事项:ini文件如果太长,读取速度会变慢,可以使用多个ini文件
//*/
// IniFile.h: interface for the CIniFile class. // //#if !defined(AFX_INIFILE_H__33CAF5C6_7820_43EB_8D84_42CC1856C35C__INCLUDED_)
#define AFX_INIFILE_H__33CAF5C6_7820_43EB_8D84_42CC1856C35C__INCLUDED_#if _MSC_VER > 1000
#pragma once #endif // _MSC_VER > 1000class CIniFile
{ public: void SetFileName(CString FileName);void WriteFileString(CString Section, CString Item, CString Value);
CString GetFileString(CString Section, CString Item, CString Value);CString GetString(CString Section, CString Item, CString Value);
int GetInt(CString Section, CString Item, int Value);void SetString(CString Section, CString Item, CString Value);
void SetInt(CString Section, CString Item, int Value);BOOL WriteIniFile();
BOOL ReadIniFile();CIniFile();
virtual ~CIniFile();//ini file
CString m_sFileName; CStdioFile IniFile; BOOL bFileExist; CStringArray FileContainer;};
#endif // !defined(AFX_INIFILE_H__33CAF5C6_7820_43EB_8D84_42CC1856C35C__INCLUDED_)
inifile.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()
{ m_sFileName = "ini.ini"; }CIniFile::~CIniFile()
{ if(FileContainer.GetSize() > 0) { FileContainer.RemoveAll(); } }void CIniFile::SetFileName(CString FileName)
{ m_sFileName = FileName; }void CIniFile::SetInt(CString Section, CString Item, int Value)
{ CString strtemp; // strtemp.Format("%d",Value); //保存到FileContainer WriteFileString(Section, Item, strtemp); //保存到ini文件 WriteIniFile(); }void CIniFile::SetString(CString Section, CString Item, CString Value)
{ //保存到FileContainer WriteFileString(Section, Item, Value); //保存到ini文件 WriteIniFile(); }CString CIniFile::GetString(CString Section, CString Item, CString Value)
{ return GetFileString(Section, Item, Value); }int CIniFile::GetInt(CString Section, CString Item, CString Value)
{ CString strtemp; // strtemp.Format("%d",Value); // return atoi(GetFileString(Section, Item, strtemp)); return GetFileString(Section, Item, strtemp); }BOOL CIniFile::ReadIniFile()
{ bFileExist=IniFile.Open(m_sFileName, CFile::modeRead);if(!bFileExist)
{ return FALSE; }CString strline;
FileContainer.RemoveAll();//将IniFile文件数据读到FileContainer
while(IniFile.ReadString(strline)) { FileContainer.Add(strline); }IniFile.Close();
return TRUE; }BOOL CIniFile::WriteIniFile()
{ bFileExist=IniFile.Open(m_sFileName, CFile::modeCreate | CFile::modeWrite); if(!bFileExist) return FALSE; CString strline;//将FileContainer写到IniFile文件
for(int i = 0; i< FileContainer.GetSize(); i++) { strline = FileContainer[i]; IniFile.WriteString(strline+"/n"); }IniFile.Close();
return bFileExist;
}CString CIniFile::GetFileString(CString Section, CString Item, CString Value)
{ ReadIniFile();//打开文件if(bFileExist == FALSE || FileContainer.GetSize() < 0)
return Value;//文件打开出错或文件为空,返回默认值int i = 0;
int iFileLines = FileContainer.GetSize(); CString strline,str;while(i<iFileLines)
{ strline = FileContainer.GetAt(i++);strline.TrimLeft();
if(strline.GetAt(0)=='[')//查找Section,第一个必须为[ { str=strline.Left(strline.Find("]"));//去掉]右边 str=str.Right(str.GetLength()-str.Find("[")-1);//去掉[左边str.TrimLeft();
str.TrimRight();if(Section == str)//找到Section
{ while(i<iFileLines) { strline = FileContainer.GetAt(i++); strline.TrimLeft();if(strline.GetAt(0)=='[')
return Value;//如果到达下一个[],即找不到,返回默认值 str = strline.Left(strline.Find("="));//去掉=右边str.TrimLeft();
str.TrimRight();if(Item == str)//找到Item
{ str=strline.Right(strline.GetLength()-strline.Find("=")-1);//去掉=左边str.TrimLeft();
str.TrimRight();return str;
} } return Value;//找不到,返回默认值 }}
}
return Value;//找不到,返回默认值 }void CIniFile::WriteFileString(CString Section, CString Item, CString Value)
{ int i = 0; int iFileLines = FileContainer.GetSize(); CString strline,str;while(i<iFileLines)
{ strline = FileContainer.GetAt(i++);strline.TrimLeft();
if(strline.GetAt(0)=='[')//查找Section,第一个必须为[ { str=strline.Left(strline.Find("]"));//去掉]右边 str=str.Right(str.GetLength()-str.Find("[")-1);//去掉[左边str.TrimLeft();
str.TrimRight();if(Section == str)//找到Section
{ while(i<iFileLines) { strline = FileContainer.GetAt(i++); strline.TrimLeft();if(strline.GetAt(0)=='[')//如果到达下一个[],即找不到Item
{ break; } str = strline.Left(strline.Find("="));//去掉=右边str.TrimLeft();
str.TrimRight();if(Item == str)//找到Item
{ strline = Item + "=" + Value; FileContainer[i-1] = strline; return; } } //找不到Item strline = Item + "=" + Value; FileContainer.InsertAt(i-1, strline); return;}
}
}
//找不到Section //直接在最后加入Section,Item,Value FileContainer.Add("[" + Section + "]"); FileContainer.Add(Item + "=" + Value); return; }