一般情况下,我们都喜欢使用ini扩展名的文件作为配置文件,可以读取及修改变量数值,也可以设置新的组,新的变量,本文的实例代码一个是读取INI的定义文件,另一个是CIniFile类实现文件,两者结合,完美实现VC++对INI文件的读写。
用户接口说明:在成员函数SetVarStr和SetVarInt函数中,当iType等于零,则如果用户制定的参数在ini文件中不存在,则就写入新的变量.当iType不等于零,则如果用户制定的参数在ini文件中不存在,就不写入新的变量,而是直接返回FALSE。程序代码如下示,部分关键点有注释加以说明:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*==================================================================
= 文件名:CIniFile类定义文件
= 主要功能:可以读取.修改变量数值,可以设置新的组,新的变量
====================================================================*/
#ifndef _CINIFILE_H_
#define _CINIFILE_H_
#include <afxtempl.h>
//用户接口说明:在成员函数SetVarStr和SetVarInt函数中,当iType等于零,则如果用户制定的参数在ini文件中不存在,
//则就写入新的变量.当iType不等于零,则如果用户制定的参数在ini文件中不存在,就不写入新的变量,而是直接返回FALSE;
class CIniFile
{
public :
CIniFile();
virtual ~CIniFile();
private :
CIniFile( const CIniFile &);
CIniFile & operator = ( const CIniFile &);
public :
//创建函数
BOOL Create( const CString &strFileName);
//得到变量整数型数值
BOOL GetVarInt( const CString &, const CString & , int &);
//得到变量字符串型数值
BOOL GetVarStr( const CString &, const CString & ,CString & );
//重新设置变量整数型数值
BOOL SetVarInt( const CString &, const CString & , const int &, const int iType = 1);
//重新设置变量字符串型数值
BOOL SetVarStr( const CString &, const CString &, const CString &, const int iType = 1);
private :
BOOL GetVar( const CString &, const CString &,CString &);
BOOL SetVar( const CString &, const CString &, const CString &, const int iType = 1);
int SearchLine( const CString &, const CString &);
private :
CArray FileContainer;
BOOL bFileExsit;
CStdioFile stfFile;
CString strInIFileName;
};
#endif
CIniFile.cpp文件内容:
#include <afxtempl.h>
#include "CIniFile.h"
CIniFile::CIniFile():bFileExsit(FALSE)
{
}
CIniFile::~CIniFile()
{
if (bFileExsit)
{
if (stfFile.Open(strInIFileName,CFile::modeCreate | CFile::modeWrite) && FileContainer.GetSize() > 0)
{
CString strParam;
for ( int i = 0; i< FileContainer.GetSize();i++)
{
strParam = FileContainer[i];
// stfFile.WriteString(strParam);
stfFile.WriteString(strParam+_T( "\n" ));
}
}
stfFile.Close();
}
if (FileContainer.GetSize() > 0)
{
FileContainer.RemoveAll();
}
}
BOOL CIniFile::Create( const CString & strFileName)
{
bFileExsit = FALSE;
strInIFileName = strFileName;
if (!stfFile.Open(strFileName,CFile::modeRead))
{
return bFileExsit;
}
CString strFileLine;
while (bFileExsit = stfFile.ReadString(strFileLine))
{
if (bFileExsit == FALSE)
return bFileExsit;
FileContainer.Add(strFileLine);
}
stfFile.Close();
bFileExsit = TRUE;
return bFileExsit;
}
BOOL CIniFile::GetVar( const CString & strSection, const CString & strVarName,CString &strReturnValue)
{
if (bFileExsit == FALSE || FileContainer.GetSize() < 0)
return bFileExsit;
int iLine = SearchLine(strSection,strVarName);
if (iLine > 0)
{
CString strParam = FileContainer[iLine -1];
strReturnValue = strParam.Mid(strParam.Find(_T( "=" )) + 1);
return TRUE;
}
return FALSE;
}
BOOL CIniFile::GetVarStr( const CString & strSection, const CString & strVarName,CString &strReturnValue)
{
return (GetVar(strSection,strVarName,strReturnValue));
}
BOOL CIniFile::GetVarInt( const CString & strSection, const CString & strVarName, int & iValue)
{
CString strReturnVar;
if (GetVar(strSection,strVarName,strReturnVar))
{
strReturnVar.TrimLeft();
int iLen = strReturnVar.GetLength();
iValue = _tstoi(strReturnVar.GetBuffer(iLen));
return TRUE;
}
return TRUE;
}
BOOL CIniFile::SetVar( const CString & strSection, const CString & strVarName, const CString & strVar, const int iType)
{
if (bFileExsit == FALSE )
return bFileExsit;
if (FileContainer.GetSize() == 0)
{
FileContainer.Add(_T( "[" ) + strSection + _T( "]" ));
FileContainer.Add(strVarName + _T( "=" ) + strVar);
return TRUE;
}
int i = 0;
int iFileLines = ( int )FileContainer.GetSize();
//for(pInterator;pInterator != FileContainer.end();++pInterator)
//{
while (i< iFileLines)
{
CString strValue = FileContainer.GetAt(i++);
strValue.TrimLeft();
if ((strValue.Find(_T( "[" )) >=0) && (strValue.Find(strSection) >=0))
{
while (i < iFileLines)
{
CString strSectionList = FileContainer[i++];
strSectionList.TrimLeft();
if (strSectionList.Find(_T( "//" )) >=0)//找到注释行
continue ;
if (strSectionList.Find(strVarName)>=0) //找到
{
CString strParam = strVarName + "=" + strVar;
//FileContainer.SetAt(i-1,strParam);
FileContainer[i-1] = strParam;
return TRUE;
}
if (strSectionList.Find(_T( "[" ),0)>=0) //在原来文件的SECTION中,没有相应的变量需要添加而且,这种情况下,下边还有别的section
{
//处理流程是这样的,首先把当前的数值依次向后移动,然后在当前位置加入新出现的数值
if (iType !=0)
return FALSE;
CString strParam;
FileContainer.Add(strParam);
int iPre = ( int )(FileContainer.GetSize()-1);
while (iPre >= i)
{
CString strBehind = FileContainer[iPre -1];
FileContainer[iPre] = strBehind;
iPre --;
}
strParam = strVarName + _T( "=" ) + strVar;
FileContainer.SetAt(i-1,strParam);
return TRUE;
}
if (i == iFileLines && iType == 0)
{
FileContainer.Add(strVarName + _T( "=" ) + strVar);
return TRUE;
}
}
}
}
if (iType == 0)
{
FileContainer.Add(_T( "[" ) + strSection + _T( "]" ));
FileContainer.Add(strVarName + _T( "=" ) + strVar);
}
return TRUE;
}
BOOL CIniFile::SetVarStr( const CString & strSection, const CString & strVarName, const CString & strValue, const int iType)
{
return SetVar(strSection,strVarName,strValue,iType);
}
BOOL CIniFile::SetVarInt( const CString & strSection, const CString & strVarName, const int & iValue, const int iType)
{
CString strVar;
strVar.Format(_T( "%d" ),iValue);
return (SetVar(strSection,strVarName,strVar,iType));
}
int CIniFile::SearchLine( const CString & strSection, const CString & strVarName)
{
if (FileContainer.GetSize() > 0)
{
int i = 0;
int iFileLines = ( int )FileContainer.GetSize();
while (i< iFileLines)
{
CString strValue = FileContainer[i++];
strValue.TrimLeft();
if (strValue.Find(_T( "[" )) >=0 && strValue.Find(strSection,1)>=0)
{
while (i < iFileLines)
{
CString strSectionList = FileContainer[i++];
strSectionList.TrimLeft();
if (strSectionList.Find(_T( "//" )) >=0)//找到注释行
continue ;
if (strSectionList.Find(strVarName)>=0) //找到
{
return i;
}
if (strSectionList.Find(_T( "[" ),0) >= 0) //另外一个段落出现,寻找失败
{
return -2;
}
}
}
}
}
return -1;
}
|