封装INI文件常见操作

#pragma once
#include <windows.h>

class INIFILE
{
private:
    char    szFilePath[260];
public:
    //构造
    INIFILE(char *FilePath,BOOL flag);
    //写入类
    void    WriteString(char *SectionName,char *DataString);
    void    AddData(char *SectionName,char *KeyName,char *Value);
    void    WriteData(char *SectionName,char *KeyName,void *lpData,UINT DataSize);
    //获取类
    DWORD    GetInteger(char *SectionName,char *KeyName);
    void    GetString(char *SectionName,char *KeyName,char *SaveStr,UINT StrSize);
    void    GetSection(char *SectionName,char *SaveStr,UINT StrSize);
    void    GetSectionName(char *SaveStr,UINT StrSize);
};

//构造函数(文件路径,不存在是否新建)
INIFILE::INIFILE(char *FilePath,BOOL flag=TRUE)
{
    HANDLE hFile;

    memset(this->szFilePath,0x00,260);
    if (flag)
        hFile=CreateFile(FilePath,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    else
        hFile=CreateFile(FilePath,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

    if (FilePath[1]!=':' && FilePath[2]!='\\'){
        GetCurrentDirectory(260,this->szFilePath);
        strcat(this->szFilePath,"\\");
        strcat(this->szFilePath,FilePath);
    }else{
        strcpy(this->szFilePath,FilePath);
    }
    CloseHandle(hFile);
}

void INIFILE::WriteString(char *SectionName,char *DataString)
{
    ::WritePrivateProfileSection(SectionName,DataString,this->szFilePath);
}

void INIFILE::AddData(char *SectionName,char *KeyName,char *Value)
{
    ::WritePrivateProfileString(SectionName,KeyName,Value,this->szFilePath);
}

void INIFILE::WriteData(char *SectionName,char *KeyName,void *lpData,UINT DataSize)
{
    ::WritePrivateProfileStruct(SectionName,KeyName,lpData,DataSize,this->szFilePath);
}

DWORD INIFILE::GetInteger(char *SectionName,char *KeyName)
{
    char ret[12]={0};

    ::GetPrivateProfileString(SectionName,KeyName,"0",ret,12,this->szFilePath);
    return (DWORD)atoi(ret);
}

void INIFILE::GetString(char *SectionName,char *KeyName,char *SaveStr,UINT StrSize)
{
    ::GetPrivateProfileString(SectionName,KeyName,"",SaveStr,StrSize,this->szFilePath);
}

void INIFILE::GetSection(char *SectionName,char *SaveStr,UINT StrSize)
{
    ::GetPrivateProfileSection(SectionName,SaveStr,StrSize,this->szFilePath);
}

void INIFILE::GetSectionName(char *SaveStr,UINT StrSize)
{
    ::GetPrivateProfileSectionNames(SaveStr,StrSize,this->szFilePath);
}
posted @ 2012-06-04 22:09  little evil  阅读(305)  评论(0编辑  收藏  举报