封装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);
}

转载于:https://www.cnblogs.com/littleevil/archive/2012/06/04/2535283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值