读写UNICODE格式的ini文件类

 

// global.h

// ========================================================================= //
//     功  能:获取当前应用程序的路径目录                                    //
//     参  数:无                                                            //
//     返回值:当前应用程序的路径目录                                        //
//     备  注:无                                                            //
// ========================================================================= //
CString GetCurrentPath();

// ========================================================================= //
//     功  能:检查给定目录是否存在,如果不存在则创建该目录                  //
//     参  数:sDirectory,给定路径                                          //
//     返回值:无                                                            //
//     备  注:无                                                            //
// ========================================================================= //
void  CheckDirectory(CString sDirectory);

// ========================================================================= //
//     功  能:返回错误信息                                                  //
//     参  数:Error,错误码                                                 //
//     返回值:错误信息                                                      //
//     备  注:无                                                            //
// ========================================================================= //
CString FormartLastError(DWORD Error);

// ========================================================================= //
//     功  能:检查目录是否存在,不存在则创建此路径,C:a                   //
//     参  数:lpszDirectory,目录路径                                       //
//     返回值:TRUE,FALSE                                                    //
//     备  注:无                                                            //
// ========================================================================= //
BOOL CheckDirectory(LPCTSTR lpszDirectory);

// ========================================================================= //
//     功  能:创建UNICODE格式的文件,以0xfeff开头                           //
//     参  数:strPathFile,文件路径                                          //
//     返回值:HANDLE,文件句柄                                              //
//     备  注:无                                                            //
// ========================================================================= //
HANDLE CreateUnicodeFile(CStringW strPathFile);

// ========================================================================= //
//     功  能:写UNICODE格式的日志文件,存放与当前应用程序路径下的log文件夹  //
//             下文件名为:log_年-月-日.txt                                  //
//     参  数:sLogMsg,日志信息                                              //
//     返回值:TRUE,FALSE                                                    //
//     备  注:无                                                            //
// ========================================================================= //
BOOL WriteLogFile(CStringW sLogMsg);


// global.cpp
#include  " global.h "
CString GetCurrentPath()
{
    TCHAR exeFullPath[MAX_PATH]; 
// MAX_PATH在API中定义了吧,好象是
    GetModuleFileName(NULL,exeFullPath,MAX_PATH);
    CString sPath;
    sPath
=exeFullPath;
    
int nPos;
    nPos
=sPath.ReverseFind ('/'); 
    sPath
=sPath.Left (nPos+1); 
    
return sPath;
}


void  CheckDirectory(CString sDirectory)
{
    
//检查目录是否存在
    WIN32_FIND_DATA fd; 
        HANDLE hFind 
= FindFirstFile(sDirectory, &fd); 
        
if (!((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
        

        ::CreateDirectory(sDirectory,NULL);
        }
 
        FindClose(hFind); 
}


CString FormartLastError(DWORD Error)
{
    LPVOID lpMsgBuf; 
    FormatMessage( 
        FORMAT_MESSAGE_ALLOCATE_BUFFER 
| 
        FORMAT_MESSAGE_FROM_SYSTEM 
| 
        FORMAT_MESSAGE_IGNORE_INSERTS, 
        NULL, 
        GetLastError(), 
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
// Default language 
        (TCHAR *&lpMsgBuf, 
        
0
        NULL 
        ); 
    TCHAR 
*p=(TCHAR*)lpMsgBuf;
    CString str
=p;
    LocalFree( lpMsgBuf ); 
    
return str;
}

BOOL CheckDirectory(LPCTSTR lpszDirectory)
{
    
//检查目录是否存在
    WIN32_FIND_DATA fd; 
    HANDLE hFind 
= FindFirstFile(lpszDirectory, &fd); 
    
if (!((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
    

        ::CreateDirectory(lpszDirectory,NULL);
    }
 
    FindClose(hFind); 
    
return TRUE;
}


HANDLE CreateUnicodeFile(CStringW strPathFile)
{
    HANDLE hFile
=NULL;
    
//创建文件
    hFile=CreateFile(strPathFile,
        GENERIC_WRITE
|GENERIC_READ,
        FILE_SHARE_READ
|FILE_SHARE_DELETE|FILE_SHARE_WRITE,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
    
if(INVALID_HANDLE_VALUE==hFile)
    
{
        AfxMessageBox(FormartLastError(GetLastError()));
        
return NULL;
    }

    DWORD dwValue
=0;
    DWORD dwSize
=0;
    dwSize 
= GetFileSize (hFile, NULL) ; 
    
if (dwSize == 0xFFFFFFFF
    

        AfxMessageBox(FormartLastError(GetLastError()));
        CloseHandle(hFile);
        
return NULL;
    }
 
    
if(dwSize==0)
    
{
        TCHAR p
=0xfeff;//UNICODE文件开头标志
        if(!WriteFile(hFile,&p,sizeof(TCHAR),&dwValue,NULL))
        
{
            AfxMessageBox(FormartLastError(GetLastError()));
            CloseHandle(hFile);
            
return NULL;
        }

    }

    
return hFile;
}


BOOL WriteLogFile(CStringW sLogMsg)
{
    CStringW sFileName;
    
if(sFileName.IsEmpty())
    
{
        sFileName
=GetCurrentPath();//获取应用程序所在目录
        sFileName=sFileName+TEXT("log/");//创建log文件夹
        CheckDirectory(sFileName);
        CString sdate;
        CTime tt
=CTime::GetTickCount();
        CString strtt
=tt.Format("log_%Y-%m-%d.txt");
        sFileName
=sFileName+strtt;
    }

    HANDLE hFile
=CreateUnicodeFile(sFileName);//创建UNICODE格式文件
    if(NULL==hFile)
    
{
        AfxMessageBox(FormartLastError(GetLastError()));
        
return FALSE;
    }

    DWORD dwValue
=0;
    DWORD dwSize
=0;
    dwSize 
= GetFileSize (hFile, NULL) ; 
    
if (dwSize == 0xFFFFFFFF
    

        AfxMessageBox(FormartLastError(GetLastError()));
        CloseHandle(hFile);
        
return FALSE;
    }

    
long logcount=0;
    
int iLength=0;
    DWORD ftype
=GetFileType(hFile);
    
if(ftype!=FILE_TYPE_DISK)//检测文件是否为磁盘文件
        return FALSE;
    TCHAR buff[
10];
    wmemset((WCHAR
*)buff,L'0',10);
    
if(dwSize!=2)//如果已经写日志
    {
        
//移动至文件开头sizeof(TCHAR)处
        DWORD p=SetFilePointer(hFile,sizeof(TCHAR),NULL,FILE_CURRENT);
        
if(p==0xFFFFFFFF)
            
return FALSE;
        
//读取日志记录数   00000000
        if(!ReadFile(hFile,buff,10*sizeof(TCHAR),&dwValue,NULL))
        
{
            AfxMessageBox(FormartLastError(GetLastError()));
            CloseHandle(hFile);
            
return FALSE;
        }

        logcount
=wcstol((WCHAR*)buff,NULL,10);
    }

    CStringW sCount;
    logcount
=logcount+1;
    sCount.Format(TEXT(
"%d"),logcount);
    
for(int i=0;i<sCount.GetLength();i++)
        buff[
8-sCount.GetLength()+i]=sCount[i];
    buff[
8]=' ';
    buff[
9]=' ';
    SetFilePointer(hFile,sizeof(TCHAR),NULL,FILE_BEGIN);
//移动至文件开头2
    
//写日志记录数
    if(!WriteFile(hFile,buff,(int)10*sizeof(TCHAR),&dwValue,NULL))
    
{
        AfxMessageBox(FormartLastError(GetLastError()));
        CloseHandle(hFile);
        
return FALSE;
    }

    SetFilePointer(hFile,NULL,NULL,FILE_END);
//移动至文件尾部
    CTime t=CTime::GetTickCount();
    CStringW sMsg
=t.Format("[%Y-%m-%d %H:%M:%S] ");
    sLogMsg
=sMsg+sLogMsg+TEXT(" ");
    iLength 
= sLogMsg.GetLength();
    
//写日志
    if(!WriteFile(hFile,sLogMsg.GetBuffer(),(int)iLength*sizeof(TCHAR), &dwValue, NULL))
    
{
        AfxMessageBox(FormartLastError(GetLastError()));
        CloseHandle(hFile);
        
return FALSE;
    }

    
//关闭文件
    CloseHandle(hFile);
    
return TRUE;
}


// IniFileReadWrite.h
#pragma once

// ini文件读写类
class  IniFileReadWrite
{
public:
    
//写ini文件,默认文件在当前程序运行目录下,文件名为set.ini
    BOOL WriteValue(LPCTSTR lpszSection,LPCTSTR lpszSectionKey,LPCTSTR lpszValue);
    BOOL WriteValue(LPCTSTR lpszSection,LPCTSTR lpszSectionKey,
int iValue);
    BOOL WriteValue(LPCTSTR lpszSection,LPCTSTR lpszSectionKey,
double dValue);
    
//读ini文件,默认文件在当前程序运行目录下,文件名为set.ini
    BOOL ReadValue(LPCTSTR lpszSection,LPCTSTR lpszSectionKey,CString &szValue);
    BOOL ReadValue(CString lpszSection,LPCTSTR lpszSectionKey,
int &iValue);
    BOOL ReadValue(CString lpszSection,LPCTSTR lpszSectionKey,
double &dValue);
    
//更改文件路径
    void SetPathFileName(LPCTSTR lpszPathFileName);
    
//构造函数
    IniFileReadWrite(LPCTSTR lpszPathFileName);
    IniFileReadWrite(
void);
    
~IniFileReadWrite(void);
private:
    
//文件路径及文件
    CString m_sPathFileName;
}
;


// IniFileReadWrite.cpp
#include  " stdafx.h "
#include 
" global.h "
#include 
" IniFileReadWrite.h "

BOOL IniFileReadWrite::WriteValue(LPCTSTR lpszSection,LPCTSTR lpszSectionKey,LPCTSTR lpszValue)
{
    
return WritePrivateProfileString(lpszSection,lpszSectionKey,lpszValue,m_sPathFileName);
}


BOOL IniFileReadWrite::WriteValue(LPCTSTR lpszSection,LPCTSTR lpszSectionKey,
int  iValue)
{
    CString sValue;
    sValue.Format(L
"%d",iValue);
    
return WriteValue(lpszSection,lpszSectionKey,sValue);
}


BOOL IniFileReadWrite::WriteValue(LPCTSTR lpszSection,LPCTSTR lpszSectionKey,
double  dValue)
{
    CString sValue;
    sValue.Format(L
"%0.2f",dValue);
    
return WriteValue(lpszSection,lpszSectionKey,sValue);
}


BOOL IniFileReadWrite::ReadValue(LPCTSTR lpszSection,LPCTSTR lpszSectionKey,CString 
& szValue)
{
    WCHAR buff[
1024];
    DWORD dwRead
=GetPrivateProfileString(lpszSection,lpszSectionKey,NULL,buff,1024,m_sPathFileName);
    
if(dwRead==0)
        
return FALSE;
    szValue
=buff;
    
return TRUE;
}


BOOL IniFileReadWrite::ReadValue(CString lpszSection,LPCTSTR lpszSectionKey,
int   & iValue)
{
    WCHAR buff[
1024];
    DWORD dwRead
=GetPrivateProfileString(lpszSection,lpszSectionKey,NULL,(LPWSTR)buff,1024,m_sPathFileName);
    
if(dwRead==0)
        
return FALSE;
    
int value=_wtoi(buff);
    iValue
=value;
    
return TRUE;
}


BOOL IniFileReadWrite::ReadValue(CString lpszSection,LPCTSTR lpszSectionKey,
double   & dValue)
{
    WCHAR buff[
1024];
    DWORD dwRead
=GetPrivateProfileString(lpszSection,lpszSectionKey,NULL,(LPWSTR)buff,1024,m_sPathFileName);
    
if(dwRead==0)
        
return FALSE;
    
double value=atof((char*)buff);
    dValue
=value;
    
return TRUE;
}


// 更改文件路径 C:a .
void  IniFileReadWrite::SetPathFileName(LPCTSTR lpszPathFileName)
{
    CheckDirectory(lpszPathFileName);
    m_sPathFileName
=lpszPathFileName;
}


IniFileReadWrite::IniFileReadWrite(LPCTSTR lpszPathFileName)
{
    m_sPathFileName
=lpszPathFileName;
    CFileFind ff;
    
if(!ff.FindFile(m_sPathFileName))
        CreateUnicodeFile(m_sPathFileName);
//创建Unicode格式文件
}


IniFileReadWrite::IniFileReadWrite(
void )
{
    
//获取当前应用程序的路径
    CStringW strPath=GetCurrentPath();
    
//文件路径
    strPath=strPath+_T("set.ini");
    CFileFind ff;
    
if(!ff.FindFile(strPath))
        CreateUnicodeFile(strPath);
//创建Unicode格式文件
    m_sPathFileName=strPath;
}


IniFileReadWrite::
~ IniFileReadWrite( void )
{
    
}


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值