//獲取文件大小
BOOL GetFileSize(IN LPCTSTR lpszFileName, OUT ULONGLONG& ullFileSize )
{
HANDLE hFile = CreateFile( lpszFileName, GENERIC_READ,
FILE_SHARE_READ,NULL, OPEN_EXISTING, 0, NULL );//打開文件
if( hFile == INVALID_HANDLE_VALUE )
return FALSE;
BY_HANDLE_FILE_INFORMATION fileInfo;//用於存放文件信息的結構
if( !GetFileInformationByHandle( hFile, &fileInfo ) )
{
CloseHandle( hFile );
return FALSE;
}
ullFileSize = fileInfo.nFileSizeHigh;
ullFileSize = ullFileSize << 32;
ullFileSize += fileInfo.nFileSizeLow;
CloseHandle(hFile);
return TRUE;
}
C++刪除文件
CString strPath =_T("E://1.txt");
if( !_taccess( strPath, 0 ) ){//文件存在
printf("文件存在\n");
int nRet = ::DeleteFile(strPath);
if( nRet ){
printf("文件刪除成功\n");
}else{
int err = ::GetLastError();
printf("文件刪除失敗,錯誤碼:[%d]\n",err);
}
}else{
printf("要刪除的文件不存在\n");
}
C++創建多級目錄
BOOL CreateMultiDirectory( CString strDir )
{
if( strDir.Right(1)!=_T("\\") )
strDir += _T("/");
//替換路徑中所有的\\爲/
if( strDir.Find(_T("\\")) != -1 )
{
strDir.Replace(_T("\\"),_T("/"));
}
int nPos = strDir.Find( _T("/") );
if( -1 == nPos )
return FALSE;
while( -1 != nPos )
{
CString strDirCur;
strDirCur = strDir.Left( nPos + 1 );
if( 0 != _taccess( strDirCur,0 ) )//判斷文件是否存在
{
if( !::CreateDirectory( strDirCur, NULL ) )
{
printf("CreateDirectory Error,ErrorCode = %d",::GetLastError());
return FALSE;
}
printf( "CreateDirectory[%s] Success.", strDirCur );
}
nPos = strDir.Find( _T("/"), nPos+1 );
}
return TRUE;
}