在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下:
一.将信息写入.INI文件中.
1.所用的WINAPI函数原型为:
BOOL WritePrivateProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName ); |
其中各参数的意义:
LPCTSTR lpAppName 是INI文件中的一个字段名.
LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:/stud/student.ini 文件中.
CString strName,strTemp; int nAge; strName="张三"; nAge=12; ::WritePrivateProfileString("StudentInfo","Name",strName,"c://stud//student.ini"); |
此时c:/stud/student.ini文件中的内容如下:
[StudentInfo]
Name=张三
3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:
strTemp.Format("%d",nAge); ::WritePrivateProfileString("StudentInfo","Age",strTemp,"c://stud//student.ini"); |
二.将信息从INI文件中读入程序中的变量.
1.所用的WINAPI函数原型为:
DWORD GetPrivateProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName ); |
其中各参数的意义:
前二个参数与 WritePrivateProfileString中的意义一样.
lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.
2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.
CString strStudName; int nStudAge; GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c://stud//student.ini"); |
执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
3.读入整型值要用另一个WINAPI函数:
UINT GetPrivateProfileInt( LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName ); |
这里的参数意义与上相同.使用方法如下:
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c://stud//student.ini"); |
三.循环写入多个值,设现有一程序,要将最近使用的几个文件名保存下来,具体程序如下:
1.写入:
CString strTemp,strTempA; int i; int nCount=6; file://共有6个文件名需要保存 for(i=0;i {strTemp.Format("%d",i); strTempA=文件名; file://文件名可以从数组,列表框等处取得. ::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA, "c://usefile//usefile.ini"); } strTemp.Format("%d",nCount); ::WritePrivateProfileString("FileCount","Count",strTemp,"c://usefile//usefile.ini"); file://将文件总数写入,以便读出. |
2.读出:
nCount=::GetPrivateProfileInt("FileCount","Count",0,"c://usefile//usefile.ini"); for(i=0;i {strTemp.Format("%d",i); strTemp="FileName"+strTemp; ::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c://usefile//usefile.ini"); file://使用strTempA中的内容. } |
补充四点:
1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.
2.文件名的路径中必须为 // ,因为在VC++中, // 才表示一个 / .
3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: ".//student.ini".
4.从网页中粘贴源代码时,最好先粘贴至记事本中,再往VC中粘贴,否则易造成编译错误,开始时我也十分不解,好好的代码怎么就不对呢?后来才找到这个方法.还有一些代码中使用了全角字符如:<,\等,也会造成编译错误.
ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息。ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Key可以赋相应的值。读写ini文件实际上就是读写某个的Section中相应的Key的值,而这只要借助几个函数即可完成。
一、向ini文件中写入信息的函数
1. 把信息写入系统的win.ini文件
BOOL WriteProfileString(
LPCTSTR lpAppName, // 节的名字,是一个以0结束的字符串
LPCTSTR lpKeyName, // 键的名字,是一个以0结束的字符串。若为NULL,则删除整个节
LPCTSTR lpString // 键的值,是一个以0结束的字符串。若为NULL,则删除对应的键
)
2. 把信息写入自己定义的.ini文件
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
LPCTSTR lpString, // 同上
LPCTSTR lpFileName // 要写入的文件的文件名。若该ini文件与程序在同一个目录下,也可使用相对路径,否则需要给出绝度路径。
)
如:
::WriteProfileString("Test","id","xym");
//在win.ini中创建一个Test节,并在该节中创建一个键id,其值为xym
::WritePrivateProfileString("Test","id","xym","d://vc//Ex1//ex1.ini");
//在Ex1目录下的ex1.ini中创建一个Test节,并在该节中创建一个键id,其值为xym
//若Ex1.ini文件与读写该文件的程序在同一个目录下,则上面语句也可写为:
::WritePrivateProfileString("Test","id","xym",".//ex1.ini");
需要注意的是,C系列的语言中,转义字符'//'表示反斜线'/'。另外,当使用相对路径时,//前的.号不能丢掉了。
二、从ini文件中读取数据的函数
1、从系统的win.ini文件中读取信息
(1) 读取字符串
DWORD GetProfileString(
LPCTSTR lpAppName, // 节名
LPCTSTR lpKeyName, // 键名,读取该键的值
LPCTSTR lpDefault, // 若指定的键不存在,该值作为读取的默认值
LPTSTR lpReturnedString, // 一个指向缓冲区的指针,接收读取的字符串
DWORD nSize // 指定lpReturnedString指向的缓冲区的大小
)
如:
CString str;
::GetProfileString("Test","id","Error",str.GetBuffer(20),20);
(2) 读取整数
UINT GetProfileInt(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
INT nDefault // 若指定的键名不存在,该值作为读取的默认值
)
如使用以下语句写入了年龄信息:
::WriteProfileString("Test","age","25");
//在win.ini中创建一个Test节,并在该节中创建一个键age,其值为25
则可用以下语句读取age键的值:
int age;
age=::GetProfileInt("Test","age",0);
2、从自己的ini文件中读取信息
(1) 读取字符串
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // 同1(1)
LPCTSTR lpKeyName, // 同1(1)
LPCTSTR lpDefault, // 同1(1)
LPTSTR lpReturnedString, // 同1(1)
DWORD nSize, // 同1(1)
LPCTSTR lpFileName // 读取信息的文件名。若该ini文件与程序在同一个目录下,也可使用相对路径,否则需要给出绝度路径。
)
如:
CString str;
::GetPrivateProfileString("Test","id","Error",str.GetBuffer(20),20,".//ex1.ini");
或:
::GetPrivateProfileString("Test","id","Error",str.GetBuffer(20),20,"d://vc//Ex1//ex1.ini");
(2) 读取整数
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
INT nDefault, // 若指定的键名不存在,该值作为读取的默认值
LPCTSTR lpFileName // 同上
)
如使用以下语句写入了年龄信息:
::WritePrivateProfileString("Test","age","25",".//ex1.ini");
//在ex1.ini中创建一个Test节,并在该节中创建一个键age,其值为25
则可用以下语句读取age键的值:
int age;
age=::GetPrivateProfileInt("Test","age",0,".//ex1.ini");
三、 删除键值或节
回顾一下WriteProfileString函数的说明
BOOL WriteProfileString(
LPCTSTR lpAppName, // 节的名字,是一个以0结束的字符串
LPCTSTR lpKeyName, // 键的名字,是一个以0结束的字符串。若为NULL,则删除整个节
LPCTSTR lpString // 键的值,是一个以0结束的字符串。若为NULL,则删除对应的键
)
由此可见,要删除某个节,只需要将WriteProfileString第二个参数设为NULL即可。而要删除某个键,则只需要将该函数的第三个参数设为NULL即可。这是删除系统的win.ini中的节或键,类似的,要删除自己定义的ini文件中的节或键,也可做相同的操作。
如:
::WriteProfileString("Test",NULL,NULL); //删除win.ini中的Test节
::WriteProfileString("Test","id",NULL); //删除win.ini中的id键
::WritePrivateProfileString("Test",NULL,NULL,".//ex1.ini"); //删除ex1.ini中的Test节
::WritePrivateProfileString("Test","id",NULL,".//ex1.ini"); //删除ex1.ini中的id键
四、如何判断一个ini文件中有多少个节
要判断一个ini文件中有多少个节,最简单的办法就是将所有的节名都找出来,然后统计节名的个数。而要将所有的节名找出来,使用GetPrivateProfileSectionNames函数就可以了,其原型如下:
DWORD GetPrivateProfileSectionNames(
LPTSTR lpszReturnBuffer, // 指向一个缓冲区,用来保存返回的所有节名
DWORD nSize, // 参数lpszReturnBuffer的大小
LPCTSTR lpFileName // 文件名,若该ini文件与程序在同一个目录下,
//也可使用相对路径,否则需要给出绝度路径
)
下面的是用来统计一个ini文件中共有多少个节的函数,当然,如果需要同时找到每个节中的各个键及其值,根据找到节名就可以很容易的得到了。
/*统计共有多少个节
节名的分离方法:若chSectionNames数组的第一字符是'/0'字符,则表明
有0个节。否则,从chSectionNames数组的第一个字符开始,顺序往后找,
直到找到一个'/0'字符,若该字符的后继字符不是 '/0'字符,则表明前
面的字符组成一个节名。若连续找到两个'/0'字符,则统计结束*/
int CTestDlg::CalcCount(void)
{
TCHAR chSectionNames[2048]={0}; //所有节名组成的字符数组
char * pSectionName; //保存找到的某个节名字符串的首地址
int i; //i指向数组chSectionNames的某个位置,从0开始,顺序后移
int j=0; //j用来保存下一个节名字符串的首地址相对于当前i的位置偏移量
int count=0; //统计节的个数
//CString name;
//char id[20];
::GetPrivateProfileSectionNames(chSectionNames,2048,".//ex1.ini");
for(i=0;i<2048;i++,j++)
{
if(chSectionNames[0]=='/0')
break; //如果第一个字符就是0,则说明ini中一个节也没有
if(chSectionNames[i]=='/0')
{
pSectionName=&chSectionNames[i-j]; //找到一个0,则说明从这个字符往前,减掉j个 偏移量,
//就是一个节名的首地址
j=-1; //找到一个节名后,j的值要还原,以统计下一个节名地址的偏移量
//赋成-1是因为节名字符串的最后一个字符0是终止符,不能作为节名
//的一部分
/*::GetPrivateProfileString(pSectionName,"id","Error",id,20,".//ex1.ini");
name.Format("%s",id);*/
//在获取节名的时候可以获取该节中键的值,前提是我们知道该节中有哪些键。
AfxMessageBox(pSectionName); //把找到的显示出来
if(chSectionNames[i+1]==0)
{
break; //当两个相邻的字符都是0时,则所有的节名都已找到,循环终止
}
}
}
return count;
}
CInc类---在VC++中读取INI文件
例子:
#include "ini.h"
// 保存数据
void Save()
{
// 打开 test.ini 文件
CIni Ini("test.ini");
// 在[index]下面写入 name=softboy
Ini.Write("index", "name","softboy");
// 在[index]下面写入 年龄=24
Ini.Write("index", "年龄", 24);
// 存盘
Ini.Save();
MessageBox("文件 test.ini 保存完毕!");
}
// 读取数据
void Load()
{
// 打开 test.ini 文件
CIni Ini("test.ini");
// 在[index]下面读取name的值
char* name = Ini.ReadText("index", "name");
// 在[index]下面读取年龄的值
long age = Ini.ReadInt("index", "年龄");
// 显示出结果
char str[64];
sprintf(str, "name:%s/n年龄:%d岁", name, age);
MessageBox(str);
// 最后不要忘记释放name
delete( name );
}
头文件:
/******************************************************************
* Ini.h
******************************************************************/
#ifndef _INI_H_
#define _INI_H_
#endif
#ifndef SAFE_DELETE
#define SAFE_DELETE(x) if( (x)!=NULL ) { delete (x); (x)=NULL; }
#endif
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(x) if( (x)!=NULL ) { delete[] (x); (x)=NULL; }
#endif
#ifndef SAFE_FREE
#define SAFE_FREE(x) if( (x)!=NULL ) { free(x); (x)=NULL; }
#endif
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(x) if( (x)!=NULL ) { (x)->Release(); (x)=NULL; }
#endif
#define ERROR_DATA -99999999
class CIni
{
// 内部数据
private:
char m_strFileName[MAX_PATH]; //文件名
long m_lDataLen; //文件长度
char* m_strData; //文件内容
int IndexNum; //索引数目([]的数目)
int *IndexList; //索引点位置列表
int Point; //当前指针
int Line, Word; //当前行列
public:
// 通用接口
CIni();
CIni(char*); //初始化打开配置文件
virtual ~CIni(); //释放内存
char *GetData(); //返回文件内容
int GetLines(int); //返回文件的行数
bool Open(char *); //打开配置文件
void Close(); //关闭配置文件
bool Save(char *filename=NULL); //保存配置文件
private:
// 内部函数
void InitIndex(); //初始化索引
int FindIndex(char *); //返回标题位置
int FindData(int, char *); //返回数据位置
int GotoNextLine(int); //提行
char *ReadDataName(int &); //在指定位置读一数据名称
char *ReadText(int); //在指定位置读字符串
bool AddIndex(char *); //加入一个索引
bool AddData(int, char *, char *); //在当前位置加入一个数据
bool ModityData(int, char *, char *); //在当前位置修改一个数据的值
int GotoLastLine(char *index); //把指针移动到本INDEX的最后一行
public:
// 公用接口
int ReadInt(char *, char *); //读一个整数
int ReadInt(char *, int ); //在指定的行读一整数
char *ReadText(char *, char *); //读一个字符串
char *ReadText(char *, int); //在指定的行读一字符串
char *ReadCaption(char *, int); //在指定行读一字符名称
bool Write(char *, char *, int); //写一个整数
bool Write(char *, char *, char *); //写一个字符串
int GetContinueDataNum(char *); //返回连续的行数(从INDEX到第一个空行)
};
实现文件:
/**************************************************************
* Ini.cpp
**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <io.h>
#include <fcntl.h>
#include <assert.h>
#include "ini.h"
// 通用接口
//初始化
CIni::CIni()
{
m_lDataLen = 0;
m_strData = NULL;
IndexNum = 0;
IndexList = NULL;
}
//初始化
CIni::CIni(char *filename)
{
m_lDataLen=0;
m_strData=NULL;
IndexNum=0;
IndexList=NULL;
Open(filename);
}
//析构释放
CIni::~CIni()
{
if( m_lDataLen != 0 )
{
SAFE_DELETE( m_strData );
m_lDataLen = 0;
}
if( IndexNum != 0 )
{
SAFE_DELETE( IndexList );
IndexNum = 0;
}
}
//读入文件
bool CIni::Open(char *filename)
{
strcpy(m_strFileName, filename);
SAFE_FREE( m_strData );
//获取文件长度
int fh;
fh = _open( filename, _O_RDONLY );
if( fh== -1 )
{
m_lDataLen = -1;
}
m_lDataLen = _filelength(fh);
_close(fh);
//文件存在
if( m_lDataLen > 0 )
{
m_strData = new char[m_lDataLen];
FILE *fp;
fp=fopen(filename, "rb");
assert( fp!=NULL );
fread(m_strData, m_lDataLen, 1, fp); //读数据
fclose(fp);
//初始化索引
InitIndex();
return true;
}
else // 文件不存在
{
// 找不到文件
m_lDataLen=1;
m_strData = new char[m_lDataLen];
memset(m_strData, 0, 1);
InitIndex();
}
return false;
}
//关闭文件
void CIni::Close()
{
if( m_lDataLen != 0 )
{
SAFE_DELETE( m_strData );
m_lDataLen = 0;
}
if( IndexNum != 0 )
{
SAFE_DELETE( IndexList );
IndexNum = 0;
}
}
//写入文件
bool CIni::Save(char *filename)
{
if( filename==NULL )
{
filename=m_strFileName;
}
FILE *fp;
fp=fopen(filename, "wb");
assert( fp!=NULL );
fwrite(m_strData, m_lDataLen, 1, fp);
fclose(fp);
return true;
}
//返回文件内容
char *CIni::GetData()
{
return m_strData;
}
//获得文件的行数
int CIni::GetLines(int cur)
{
int n=1;
for(int i=0; i<cur; i++)
{
if( m_strData[i]=='/n' )
n++;
}
return n;
}
// 内部函数
//计算出所有的索引位置
void CIni::InitIndex()
{
IndexNum=0;
for(int i=0; i<m_lDataLen; i++)
{
//找到
if( m_strData[i]=='[' && ( m_strData[i-1]=='/n' || i==0 ) )
{
IndexNum++;
}
}
//申请内存
SAFE_DELETE( IndexList );
if( IndexNum>0 )
IndexList=new int[IndexNum];
int n=0;
for(i=0; i<m_lDataLen; i++)
{
if( m_strData[i]=='[' && ( m_strData[i-1]=='/n' || i==0 ) )
{
IndexList[n]=i+1;
n++;
}
}
}
//返回指定标题位置
int CIni::FindIndex(char *string)
{
for(int i=0; i<IndexNum; i++)
{
char *str=ReadText( IndexList[i] );
if( strcmp(string, str) == 0 )
{
SAFE_FREE( str );
return IndexList[i];
}
SAFE_FREE( str );
}
return -1;
}
//返回指定数据的位置
int CIni::FindData(int index, char *string)
{
int p=index; //指针
while(1)
{
p=GotoNextLine(p);
char *name=ReadDataName(p);
if( strcmp(string, name)==0 )
{
SAFE_FREE( name );
return p;
}
SAFE_FREE( name );
if( p>=m_lDataLen ) return -1;
}
return -1;
}
//提行
int CIni::GotoNextLine(int p)
{
for(int i=p; i<m_lDataLen; i++)
{
if( m_strData[i]=='/n' )
return i+1;
}
return i;
}
//在指定位置读一数据名称
char *CIni::ReadDataName(int &p)
{
char chr;
char *Ret;
int m=0;
Ret=new char[64];
memset(Ret, 0, 64);
for(int i=p; i<m_lDataLen; i++)
{
chr = m_strData[i];
//结束
if( chr == '/r' )
{
p=i+1;
return Ret;
}
//结束
if( chr == '=' || chr == ';' )
{
p=i+1;
return Ret;
}
Ret[m]=chr;
m++;
}
return Ret;
}
//在指定位置读一字符串
char *CIni::ReadText(int p)
{
char chr;
char *Ret;
int n=p, m=0;
int LineNum = GotoNextLine(p) - p + 1;
Ret=new char[LineNum];
memset(Ret, 0, LineNum);
for(int i=0; i<m_lDataLen-p; i++)
{
chr = m_strData[n];
//结束
if( chr == ';' || chr == '/r' || chr == '/t' || chr == ']' )
{
//ShowMessage(Ret);
return Ret;
}
Ret[m]=chr;
m++;
n++;
}
return Ret;
}
//加入一个索引
bool CIni::AddIndex(char *index)
{
char str[256];
memset(str, 0, 256);
int n=FindIndex(index);
if( n == -1 ) //新建索引
{
sprintf(str,"/r/n[%s]/r/n",index);
m_strData = (char *)realloc(m_strData, m_lDataLen+strlen(str)); //重新分配内存
sprintf(&m_strData[m_lDataLen], "%s", str);
m_lDataLen+=strlen(str);
InitIndex();
return true;
}
return false; //已经存在
}
//在当前位置加入一个数据
bool CIni::AddData(int p, char *name, char *string)
{
char *str;
int len=strlen(string);
str=new char[len+256];
memset(str, 0, len+256);
sprintf(str,"%s=%s/r/n",name,string);
len=strlen(str);
p=GotoNextLine(p); //提行
m_strData = (char *)realloc(m_strData, m_lDataLen+len); //重新分配内存
char *temp=new char[m_lDataLen-p];
memcpy(temp, &m_strData[p], m_lDataLen-p);
memcpy(&m_strData[p+len], temp, m_lDataLen-p); //把后面的搬到末尾
memcpy(&m_strData[p], str, len);
m_lDataLen+=len;
SAFE_DELETE( temp );
SAFE_DELETE( str );
return true;
}
//在当前位置修改一个数据的值
bool CIni::ModityData(int p, char *name, char *string)
{
int n=FindData(p, name);
char *t=ReadText(n);
p=n+strlen(t);
if( strlen(t)>0 ) free(t);
int newlen=strlen(string);
int oldlen=p-n;
m_strData = (char *)realloc(m_strData, m_lDataLen+newlen-oldlen); //重新分配内存
char *temp=new char[m_lDataLen-p];
memcpy(temp, &m_strData[p], m_lDataLen-p);
memcpy(&m_strData[n+newlen], temp, m_lDataLen-p); //把后面的搬到末尾
memcpy(&m_strData[n], string, newlen);
m_lDataLen+=newlen-oldlen;
SAFE_DELETE( temp );
return true;
}
//把指针移动到本INDEX的最后一行
int CIni::GotoLastLine(char *index)
{
int n=FindIndex(index);
n=GotoNextLine(n);
while(1)
{
if( m_strData[n] == '/r' || m_strData[n] == EOF || m_strData[n] == -3 || m_strData[n] == ' ' || m_strData[n] == '/' || m_strData[n] == '/t' || m_strData[n] == '/n' )
{
return n;
}
else
{
n=GotoNextLine(n);
if( n >= m_lDataLen ) return n;
}
}
}
/
// 对外接口
/
//以普通方式读一字符串数据
char *CIni::ReadText(char *index, char *name)
{
int n=FindIndex(index);
assert( n != -1 );
int m=FindData(n, name);
assert( m != -1 );
return ReadText(m);
}
//在指定的行读一字符串
char *CIni::ReadText(char *index, int lines)
{
int n=FindIndex(index);
assert( n != -1 );
//跳到指定行数
n=GotoNextLine(n);
for(int i=0; i<lines; i++)
{
if( n<m_lDataLen )
n=GotoNextLine(n);
}
//读数据
while( n<=m_lDataLen )
{
if( m_strData[n] == '=' )
{
n++;
return ReadText(n);
}
if( m_strData[n] == '/r' )
{
return "";
}
n++;
}
return "";
}
//以普通方式读一整数
int CIni::ReadInt(char *index, char *name)
{
int n=FindIndex(index);
assert( n != -1 );
int m=FindData(n, name);
assert( m != -1 );
char *str=ReadText(m);
int ret=atoi(str);
free(str);
return ret;
}
//在指定的行读一整数
int CIni::ReadInt(char *index, int lines)
{
int n=FindIndex(index);
assert( n != -1 );
//跳到指定行数
n=GotoNextLine(n);
for(int i=0; i<lines; i++)
{
if( n<m_lDataLen )
n=GotoNextLine(n);
}
//读数据
while( n<m_lDataLen )
{
if( m_strData[n] == '=' )
{
n++;
char *str=ReadText(n);
int ret=atoi(str);
free(str);
return ret;
}
if( m_strData[n] == '/r' )
{
return ERROR_DATA;
}
n++;
}
return ERROR_DATA;
}
//在指定的行读一数据名称
char *CIni::ReadCaption(char *index, int lines)
{
int n=FindIndex(index);
assert( n != -1 );
//跳到指定行数
n=GotoNextLine(n);
for(int i=0; i<lines; i++)
{
if( n<m_lDataLen )
n=GotoNextLine(n);
}
return ReadDataName(n);
}
//以普通方式写一字符串数据
bool CIni::Write(char *index, char *name, char *string)
{
int n=FindIndex(index);
if( n == -1 ) //新建索引
{
AddIndex(index);
n=FindIndex(index);
n=GotoLastLine(index);
AddData(n, name, string); //在当前位置n加一个数据
return true;
}
//存在索引
int m=FindData(n, name);
if( m==-1 ) //新建数据
{
n=GotoLastLine(index);
AddData(n, name, string); //在当前位置n加一个数据
return true;
}
//存在数据
ModityData(n, name, string); //修改一个数据
return true;
}
//以普通方式写一整数
bool CIni::Write(char *index, char *name, int num)
{
char string[32];
sprintf(string, "%d", num);
int n=FindIndex(index);
if( n == -1 ) //新建索引
{
AddIndex(index);
n=FindIndex(index);
n=GotoLastLine(index);
AddData(n, name, string); //在当前位置n加一个数据
return true;
}
//存在索引
int m=FindData(n, name);
if( m==-1 ) //新建数据
{
n=GotoLastLine(index);
AddData(n, name, string); //在当前位置n加一个数据
return true;
}
//存在数据
ModityData(n, name, string); //修改一个数据
return true;
}
//返回连续的行数
int CIni::GetContinueDataNum(char *index)
{
int num=0;
int n=FindIndex(index);
n=GotoNextLine(n);
while(1)
{
if( m_strData[n] == '/r' || m_strData[n] == EOF || m_strData[n] == -3 || m_strData[n] == ' ' || m_strData[n] == '/' || m_strData[n] == '/t' || m_strData[n] == '/n' )
{
return num;
}
else
{
num++;
n=GotoNextLine(n);
if( n >= m_lDataLen ) return num;
}
}
}