开源项目BNBT可用于其他项目中的文件操作部分

ExpandedBlockStart.gif View Code
#include  " stdafx.h "
#include 
< string .h >
#include 
< iostream >
#include 
" time.h "
using   namespace  std;


void  UTIL_LogPrint(  const   char   * format, ... )
{

    ::OutputDebugString(format);
}
void  UTIL_DeleteFile(  const   char   * szFile )
{
    
if ( unlink( szFile )  ==   0  )
        UTIL_LogPrint( 
" deleted \"%s\"\n " , szFile );
    
else
    {
#ifdef WIN32
        UTIL_LogPrint( 
" error deleting \"%s\"\n " , szFile );
#else
        UTIL_LogPrint( 
" error deleting \"%s\" - %s\n " , szFile, strerror( errno ) );
#endif
    }
}


void  UTIL_MakeFile(  const   char   * szFile,  string  strContents )
{
    FILE 
* pFile  =  NULL;

    
if ( ( pFile  =  fopen( szFile,  " wb "  ) )  ==  NULL )
    {
        UTIL_LogPrint( 
" warning - unable to open %s for writing\n " , szFile );

        
return ;
    }

    fwrite( (
void   * )strContents.c_str( ),  sizeof char  ), strContents.size( ), pFile );
    fclose( pFile );
}

bool  UTIL_CheckFile(  const   char   * szFile )
{
    
//  check if file exists

    FILE 
* pFile  =  NULL;

    
if ( ( pFile  =  fopen( szFile,  " r "  ) )  ==  NULL )
        
return   false ;

    fclose( pFile );

    
return   true ;
}


string  UTIL_ReadFile(  const   char   * szFile )
{
    FILE 
* pFile  =  NULL;

    
if ( ( pFile  =  fopen( szFile,  " rb "  ) )  ==  NULL )
    {
        UTIL_LogPrint( 
" warning - unable to open %s for reading\n " , szFile );

        
return   string ( );
    }
    UTIL_LogPrint( 
" open %s for reading\n " , szFile );

    fseek( pFile, 
0 , SEEK_END );
    unsigned 
long  ulFileSize  =  ftell( pFile );
    fseek( pFile, 
0 , SEEK_SET );
    
char   * pData  =  ( char   * )malloc(  sizeof char  )  *  ulFileSize );
    memset( pData, 
0 sizeof char  )  *  ulFileSize );
    fread( (
void   * )pData,  sizeof char  ), ulFileSize, pFile );
    fclose( pFile );
    
string  strFile( pData, ulFileSize );
    free( pData );

    
return  strFile;
}


void  UTIL_MoveFile(  const   char   * szFile,  const   char   * szDest )
{
    
if ( UTIL_CheckFile( szDest ) )
        UTIL_LogPrint( 
" error archiving \"%s\" - destination file already exists\n " , szDest );
    
else
        UTIL_MakeFile( szDest, UTIL_ReadFile( szFile ) );

    
//  thanks MrMister

    UTIL_DeleteFile( szFile );
}

void  UTIL_CopyFile(  const   char   * szFile,  const   char   * szDest )
{
    
if ( UTIL_CheckFile( szDest ) )
        UTIL_LogPrint( 
" error archiving \"%s\" - destination file already exists\n " , szDest );
    
else
        UTIL_MakeFile( szDest, UTIL_ReadFile( szFile ) );

}


string  UTIL_GetLocalTimeString() // edited 不保证跨平台性

{
    time_t rawtime;
    
struct  tm  *  timeinfo;
    time ( 
& rawtime );
    timeinfo 
=  localtime (  & rawtime ); 
    
char  year[ 5 ];
    
char  mon[ 3 ];
    
char  day[ 3 ];
    
char  hour[ 3 ];
    
    
char  min[ 3 ];
    
char  sec[ 3 ];

    strftime(year,
5 , " %Y " ,timeinfo);
    strftime(mon,
3 , " %m " ,timeinfo);
    strftime(day,
3 , " %d " ,timeinfo);
    strftime(hour,
3 , " %H " ,timeinfo);
    strftime(min,
3 , " %M " ,timeinfo);
    strftime(sec,
3 , " %S " ,timeinfo);
     
    
string  timestr;
    timestr.append(year);
    timestr.append(mon);
    timestr.append(day);
    timestr.append(hour);
    timestr.append(min);
    timestr.append(sec);

    
return  timestr;
}

void  CreateDir( const   char   * DirPath) // edited 不保证跨平台性
{
    CreateDirectory(DirPath,NULL);
}

转载于:https://www.cnblogs.com/no7dw/archive/2011/05/18/2050381.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值