c++写配置文件

  1. 类定义

// ini.h: interface for the Cini class.
#if !defined(AFX_INI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)
#define AFX_INI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//#include <afxwin.h>
class __declspec(dllexport)Cini  
{
public:
    static DWORD ReadString (char *section, char * key,  char* stringtoread  ,  char * filename);
    static BOOL  WriteString(LPCTSTR section, LPCTSTR key,char* stringtoadd, char *filename);
    Cini();
    virtual ~Cini();
};
#endif // !defined(AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)

  1. 类实现

// ini.cpp: implementation of the Cini class.
#include "stdafx.h"
#include "ini.h" 
//#include "vld.h"
/
//Cini类的构造函数和析构函数
Cini::Cini()
{
}
Cini::~Cini()
{
}
/
//写字符串到INI文件,GetLastError()函数用于返回写入失败
void error(LPSTR lpszFunction) 
{ 
    CHAR szBuf [80] ; 
    DWORD dw = GetLastError(); 
    sprintf_s(szBuf, 80, "%s failed: GetLastError returned %u\n",lpszFunction, dw); 
    MessageBox(NULL, szBuf, "Error", MB_OK); 
    ExitProcess(dw); 
}
BOOL Cini::WriteString(LPCTSTR section, LPCTSTR key, char *stringtoadd, char *filename)
{
   CHAR FilePath[255]; 
 memset(FilePath, 0 ,255);
 HMODULE hModule = ::GetModuleHandle(NULL);
 if (hModule != NULL)
 {
  ::GetModuleFileName(hModule,FilePath,255); 
  //GetProccessImageFileName(PROCESS_QUERY_INFORMATION ,FilePath,255);
  (strrchr(FilePath,'\\'))[1] = 0; 
  strcat(FilePath,filename);
 }
    return ::WritePrivateProfileString(section,key,stringtoadd,FilePath);
}
/
//从INI文件中读取字符串
DWORD Cini::ReadString(char *section, char * key,  char* stringtoread  ,  char * filename)
{
  CHAR FilePath[255]; 
 memset(FilePath, 0 ,255);
 HMODULE hModule = ::GetModuleHandle(NULL);
 if (hModule != NULL)
 {
  ::GetModuleFileName(hModule,FilePath,255); 
  //GetProccessImageFileName(PROCESS_QUERY_INFORMATION ,FilePath,255);
  (strrchr(FilePath,'\\'))[1] = 0; 
  strcat(FilePath,filename);
 }
    return ::GetPrivateProfileString(section, key,NULL,stringtoread,255,FilePath);
}

转载于:https://my.oschina.net/qihaitao/blog/395078

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值