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


    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( ), sizeofchar ), 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( sizeofchar ) * ulFileSize );
        memset( pData, 
    0sizeofchar ) * ulFileSize );
        fread( (
    void *)pData, sizeofchar ), 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);
    }
  • 相关阅读:
    1.两数之和 力扣,水题
    525.连续数组 力扣 (前缀和)
    [LeetCode]56. Group Anagrams变位词分组
    界面布局注意(一)
    docker常用命令
    docker常用批量操作命令
    Golang package之math/rand
    (三)虚拟机与Linux新尝试——20155306白皎
    洛谷 P1383 codevs 3333 高级打字机
    BZOJ 1013 cogs 1845 [JSOI2008]球形空间产生器sphere
  • 原文地址:https://www.cnblogs.com/no7dw/p/2050381.html
Copyright © 2020-2023  润新知