现在的位置: 首页 > 综合 > 正文

C++ INI文件

2018年06月06日 ⁄ 综合 ⁄ 共 3548字 ⁄ 字号 评论关闭

INI文件多用于存储程序的初始化信息。例如,记录程序连接数据库的名称、上一次用户登录的名称、用户的注册信息等。

一.
INI 文件格式


复制代码
[Section1 Name]
KeyName1=value1
KeyName2=value2
...... ......

[Section2 Name]
KeyName1=value1
KeyName2=value2
复制代码

  Section:节名;KeyName:键名;value:键值。对于一个INI文件,可以有多个节,每个节可以包含多个键。

二.读写INI文件


1. GetPrivateProfileInt - 从INI文件的指定Section 指定key 的整数值

复制代码
GetPrivateProfileInt(
LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
INT nDefault // 如果 Key 值没有找到,则返回缺省的值是多少
LPCTSTR lpFileName // ini 文件的文件名
);
复制代码

2. GetPrivateProfileString - 从INI文件的指定Section 指定key 的串值

复制代码
GetPrivateProfileString(
LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
LPCTSTR lpDefault, // 如果 Key 值没有找到,则返回缺省的字符串的地址
LPTSTR lpReturnedString, // 返回字符串的缓冲区地址
DWORD nSize // 缓冲区的长度
LPCTSTR lpFileName // ini 文件的文件名
);
复制代码

3. GetPrivateProfileSection - 从INI文件中读出指定Section 的内容

复制代码
GetPrivateProfileSection(
LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
LPTSTR lpReturnedString, // 返回数据的缓冲区地址
DWORD nSize // 返回数据的缓冲区长度
LPCTSTR lpFileName // ini 文件的文件名
);
复制代码

4. GetPrivateProfileSectionNames - 从INI文件中获得所有Section信息

GetPrivateProfileSectionNames(
LPTSTR lpszReturnBuffer, // 返回数据的缓冲区地址
DWORD nSize // 返回数据的缓冲区长度
LPCTSTR lpFileName // ini 文件的文件名
);

5. WritePrivateProfileSection - 将整个Section 的内容写入INI文件的指定Section中

WritePrivateProfileSection(
LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
LPCTSTR lpString // 要写入的数据的地址
LPCTSTR lpFileName // ini 文件的文件名
);

6. WritePrivateProfileString - 将指定Key的串值写入INI文件的指定Section中

复制代码
WritePrivateProfileString(
LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
LPCTSTR lpString // 要写的字符串地址
LPCTSTR lpFileName // ini 文件的文件名
);
复制代码

三.应用


  在我们实际使用的时候,用的最多的是GetPrivateProfileString和WritePrivateProfileString,但在对自定义INI文件操作的时候要注意的是,如果lpFileName指定的文件不包含路径信息,API将默认为Windows 的安装目录而不是系统当前路径,比较方便的方法是使用.\ xxx.ini 作为文件名称指示API操作对象为当前路径下的xxx.ini文件。

(1)保存

复制代码
void CRemoteMgrDlg::OnSave() 
{
    // TODO: Add your control notification handler code here    
    CString IP_Save;
    char inBuf[256];
    char path[255] ;
    m_CtrlIP.GetWindowText(IP_Save);                                        
    
    if(strcmp(IP_Save,m_IPData)!=0)                                            //IP地址有改变
    {
        GetCurrentDirectory(256,inBuf);        
        sprintf(path,"%s\\data.ini",inBuf);                                    //设置ini文件的全路径
        
        WritePrivateProfileString("信息","IP地址",IP_Save,path);
        strcpy(m_IPData,IP_Save);
        MessageBox("保存成功!","提示");         
    }    
}
复制代码

(2)读取加载

复制代码
CString CRemoteMgrDlg::Data_Loading() 
{
    char path[255] ;
    char inBuf[256];
    CString IP_Save;

    GetCurrentDirectory(256,inBuf);        
    sprintf(path,"%s\\data.ini",inBuf);                                                    //设置ini文件的全路径
    
    GetPrivateProfileString("信息","IP地址","",IP_Save.GetBuffer(255),255,path);        //读取数据
    m_CtrlIP.SetWindowText(IP_Save);    
    m_CtrlPort.SetWindowText("8888");
    
    strcpy(m_IPData,IP_Save);
    return IP_Save;
}
复制代码

(3)删除节名下的内容

  如果保存的数据比较多,并且每次保存的数据个数不定,可能会要求删除这个节名的内容,然后再进行数据的保存。

复制代码
BOOL CRemoteMgrDlg::DelSection(LPCTSTR lpSection) 
{ 
    char inBuf[256];
    char path[255] ;
    GetCurrentDirectory(256,inBuf);        
    sprintf(path,"%s\\data.ini",inBuf);    

    if(WritePrivateProfileString(lpSection,NULL,NULL,path)) 
        return FALSE; 
    else 
        return GetLastError(); 
} 
复制代码

(4)具有相同性质的数组类数据写入

  可以先写入这个数组的长度,然后for循环写入数组的内容

复制代码
BOOL CIniFile::Write(LPCSTR AppName, LPCSTR KeyName, LONG RowIndex, LPCSTR lpString)
{
    CHAR    MKeyName[64];
    sprintf(MKeyName,"%s_%ld",KeyName,RowIndex);
    return Write(AppName,MKeyName,lpString);
}
BOOL CIniFile::Write(LPCSTR AppName, LPCSTR KeyName, LPCSTR lpString)
{
    return WritePrivateProfileString(AppName, KeyName, lpString,m_FileName);
}
复制代码

(4)具有相同性质的数组类数据读取

  可以先读出这个数组的长度,然后for循环读出数组的内容

复制代码
DWORD CIniFile::Read(LPCSTR AppName, LPCSTR KeyName, LONG RowIndex, LPCSTR lpDefault, LPSTR ReturnedString, DWORD nSize)
{
    CHAR    MKeyName[64];
    sprintf(MKeyName,"%s_%ld",KeyName,RowIndex);
    return Read(AppName,MKeyName,lpDefault,ReturnedString,nSize);
}
DWORD CIniFile::Read(LPCSTR AppName, LPCSTR KeyName, LPCSTR lpDefault, LPSTR ReturnedString, DWORD nSize)
{
    return GetPrivateProfileString(AppName,KeyName,lpDefault,ReturnedString,nSize,m_FileName); 
}
复制代码

   当然,对于数据的读写,也可以用普通文件的方式来完成。

抱歉!评论已关闭.