开源项目BNBT可用于其他项目中的文件操作部分
2011-05-18 19:28
501 查看
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);
}
#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);
}
相关文章推荐
- 基于.NET的开源项目NPOI,操作XLS文件不错
- 【开源项目研究】UMLet------配置文件操作
- ajax的项目实操(只用于记录部分文件未引入)
- Fbx文件的加载与渲染:一个DirectX11小项目开源啦,主要用来演示fbx文件的加载与渲染
- IOS开发之----Xcode非ARC项目中设置部分文件ARC支持
- 第十三周项目六 体验文件操作(5)
- 第十三周项目6-体验文件操作 2
- 【风】的ACE笔记(10)-ACE文件操作(部分转载)
- 第13周上机实践项目6——体验文件操作(2-实战)
- [开源项目发布]Observer:根据map文件测试程序在运行中各个函数的运行时间
- 一款使用C# .NET开发的SIP客户端开源项目含完整源码项目文件
- 收集一些.NET开发资源站点和部分优秀.NET开源项目
- Linux学习之CentOS(四)----Linux文件属性、所有者、群组、其他组及文件权限操作简要总结
- 文件格式相关开源项目
- php部分--文件操作
- 昨天发现的nginx默认不支持下划线引起angular-phonecat项目部分文件404问题
- 转载Prefix.pch文件说明,以及分享兴风作浪哥的开源项目链接
- <车载物联网项目,视频采集传输部分,第二天> 使用ffserver输出视频文件流,并且使用使用命令行终端播放视频文件
- Spring 的优秀工具类盘点,第 1 部分: 文件资源操作和 Web 相关工具类
- java web项目时出现Jax-Ws-Web Services等几个文件,而且其他项目存在问题解决