📄 dirfile.cpp
字号:
#include "StdAfx.h"
#include "DirFile.h"
/********************************************************************************
* Function Type : Global
* Parameter : filename - 文件名
* data - 要保存的数据
* mode - 文件打开的模式
* size - 数据大小
* nStartPos - 文件开始位置
* Return Value : >=0 - 写入文件的大小
* -1 - 写操作失败
* Description : 保存数据到文件
*********************************************************************************/
int WriteDataToFile(LPCTSTR filename,void* data,long size,LPCTSTR mode, int nStartPos/*=-1*/ )
{
char szFileName[MAX_PATH] = {0};
char szMode[8] = {0};
if ( GetMltiByteChar ( filename, szFileName, sizeof(szFileName) ) < 1 )
return -1;
GetMltiByteChar(mode,szMode,sizeof(szMode));
FILE *fp;
long retval;
fp=fopen((const char*)szFileName,(const char*)szMode );
if ( fp!=NULL)
{
if ( nStartPos >= 0 )
{
if ( fseek ( fp, nStartPos, SEEK_SET ) != 0 )
return -1;
}
retval = (long)fwrite(data,sizeof(char),size,fp);
fclose(fp);
if(retval != size)
{
return -1;
}
else return retval;
}
else
{
return -1;
}
}
/********************************************************************************
* Function Type : Global
* Parameter : filename - 文件名
* data - 读到的数据存于此缓冲
* size - 缓冲大小
* nStartPos - 文件开始位置
* Return Value : >=0 - 读到数据的大小
* -1 - 操作失败
* Description : 从文件中读取数据
*********************************************************************************/
int ReadDataFromFile(LPCTSTR filename,void* data,long size, int nStartPos/*=-1*/)
{
char szFileName[MAX_PATH] = {0};
if ( GetMltiByteChar ( filename, szFileName, sizeof(szFileName) ) < 1 )
return -1;
FILE *fp;
long retval;
fp=fopen((const char*)szFileName,"rb");
if ( fp!=NULL)
{
if ( nStartPos >= 0 )
{
if ( fseek ( fp, nStartPos, SEEK_SET ) != 0 )
return -1;
}
retval = (long)fread(data,sizeof(char), size, fp);
fclose(fp);
if ( retval >= 0 ) return retval;
}
return -1;
}
//
// 标准化路径或文件名,把不符合文件名命名规则的字符替换成指定的字符。
// 加上该函数
//
CString StandardizationFileForPathName ( LPCTSTR lpszFileOrPathName, BOOL bIsFileName, char cReplaceChar/*=_T('_')*/ )
{
CString csFileOrPathName = GET_SAFE_STRING(lpszFileOrPathName);
CString csHead, csTail;
// 路径名中最后一个'\\'是正常的。另外类似“c:\\”的字符也是正常的。所以先提取出来不参与后面的替换,等替换完以后再补回来
if ( !bIsFileName )
{
if ( csFileOrPathName.GetLength() >= 1 && (csFileOrPathName[csFileOrPathName.GetLength()-1] == _T('\\') || csFileOrPathName[csFileOrPathName.GetLength()-1] == _T('/')) )
{
csTail += csFileOrPathName[csFileOrPathName.GetLength()-1];
csFileOrPathName = csFileOrPathName.Left ( csFileOrPathName.GetLength()-1 );
}
if ( csFileOrPathName.GetLength() >= 2 && isalpha(csFileOrPathName[0]) && csFileOrPathName[1]==_T(':') )
{
csHead = csFileOrPathName.Left(2);
csFileOrPathName = csFileOrPathName.Mid(2);
}
if ( csFileOrPathName.GetLength() >= 1 && (csFileOrPathName[0]==_T('\\') || csFileOrPathName[0]==_T('/')) )
{
csHead += csFileOrPathName[0];
csFileOrPathName = csFileOrPathName.Mid(1);
}
}
else
{
csFileOrPathName.Replace ( _T("\\"), _T("_") );
csFileOrPathName.Replace ( _T("/"), _T("_") );
}
csFileOrPathName.Replace ( _T(":"), _T("_") );
csFileOrPathName.Replace ( _T("*"), _T("_") );
csFileOrPathName.Replace ( _T("?"), _T("_") );
csFileOrPathName.Replace ( _T("\""), _T("_") );
csFileOrPathName.Replace ( _T("<"), _T("_") );
csFileOrPathName.Replace ( _T(">"), _T("_") );
csFileOrPathName.Replace ( _T("|"), _T("_") );
csFileOrPathName.Insert ( 0, csHead );
csFileOrPathName += csTail;
return csFileOrPathName;
}
//
// 标准化路径缓冲,如果不是以“\”结尾,将自动加上
//
void StandardizationPathBuffer ( LPTSTR szPath, int nSize, TCHAR cFlagChar/*=_T('\\')*/ )
{
int nLen = (int)strlen_s(szPath);
if ( nLen < 1 ) return;
ASSERT_ADDRESS ( szPath, nLen+1 );
TCHAR szTemp[4] = {0};
szTemp[0] = cFlagChar;
if ( szPath[nLen-1] != cFlagChar )
strncat_s ( szPath, szTemp, nSize/sizeof(TCHAR) );
CString csPath = StandardizationFileForPathName ( szPath, FALSE );
strncpy_s ( szPath, csPath, nSize );
}
CString StandardizationPathBuffer ( LPTSTR lpszPath, TCHAR cFlagChar/*=_T('\\')*/ )
{
TCHAR szPath[MAX_PATH] = {0};
strncpy_s ( szPath, lpszPath, LENGTH(szPath) );
StandardizationPathBuffer ( (LPTSTR)szPath, (int)sizeof(szPath), (TCHAR)cFlagChar );
return szPath;
}
//
//
// 从一个完整的全路径名(包含文件名)中分离出路径(没有文件名)和
// 文件名,如:从“E:\001\002.exe”中分得“E:\001\”,结果存入到
// lsOnlyPath中,“002.exe”存入szOnlyFileName中
//
BOOL PartFileAndPathByFullPath (
IN LPCTSTR lpszFilePath, // 全路径名(包含文件名)
OUT TCHAR *szOnlyFileName, // 光文件名(没有路径)
int nFileNameSize,
OUT TCHAR *szOnlyPath /*=NULL*/, // 光路径(没有文件名)
int nPathSize/*=0*/
)
{
ASSERT ( lpszFilePath );
TCHAR chDirPart = _T('\\');
if ( strchr_s ( lpszFilePath, _T('/') ) )
chDirPart = _T('/');
if ( szOnlyFileName )
{
memset ( szOnlyFileName, 0, nFileNameSize );
}
if ( szOnlyPath )
{
memset ( szOnlyPath, 0, nPathSize );
}
WIN32_FILE_ATTRIBUTE_DATA FileAttrData;
if ( GetFileAttributesEx ( lpszFilePath, GetFileExInfoStandard, (LPVOID)&FileAttrData ) &&
( FileAttrData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY
&& FileAttrData.dwFileAttributes != 0xffffffff ) // 本身就是目录
{
if ( szOnlyPath )
{
strncpy_s ( szOnlyPath, lpszFilePath, nPathSize/sizeof(TCHAR) );
StandardizationPathBuffer ( szOnlyPath, nPathSize, chDirPart );
}
return TRUE;
}
LPTSTR p = (LPTSTR)strrchr_s ( lpszFilePath, chDirPart );
if ( !p )
{
strncpy_s ( szOnlyFileName, lpszFilePath, nFileNameSize/sizeof(TCHAR) );
return TRUE;
}
if ( szOnlyFileName )
strncpy_s ( szOnlyFileName, p+1, nFileNameSize/sizeof(TCHAR) );
if ( szOnlyPath )
{
strncpy_s ( szOnlyPath, lpszFilePath, nPathSize/sizeof(TCHAR) );
int nLen = (int)(p-lpszFilePath+1);
if ( nPathSize-1 < nLen ) return FALSE;
szOnlyPath [ nLen ] = _T('\0');
}
return TRUE;
}
/********************************************************************************
* Function Type : Global
* Parameter : lpFileName - 文件路径
* Return Value : -1 - 失败
* >=0 - 文件大小
* Description : 获取文件属性来 ( 文件大小、创建时间 )
*********************************************************************************/
int hwGetFileAttr ( LPCTSTR lpFileName, DWORD *p_dwCreateTime/*=NULL*/ )
{
WIN32_FILE_ATTRIBUTE_DATA FileAttrData;
BOOL bRet = GetFileAttributesEx ( lpFileName,
GetFileExInfoStandard, (LPVOID)&FileAttrData );
if ( !bRet ) return 0;
if ( ( FileAttrData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY ) // 是目录
{
return 0;
}
if ( p_dwCreateTime )
{
CTime t ( FileAttrData.ftCreationTime );
*p_dwCreateTime = (DWORD)t.GetTime();
}
return ( GETFILESIZE ( FileAttrData ) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -