首页 > 代码库 > 创建指定大小的空文件

创建指定大小的空文件

 1 BOOL CreateFileDemo(TCHAR* pFileName, DWORD dwSize) 2 { 3     HANDLE hFile; 4     HANDLE hMapFile; 5  6     hFile = CreateFile( 7         pFileName, 8         GENERIC_WRITE | GENERIC_READ, 9         FILE_SHARE_READ,10         NULL,11         CREATE_ALWAYS,12         FILE_ATTRIBUTE_NORMAL,13         NULL14         );15     if( hFile == INVALID_HANDLE_VALUE )16     {17         OutputDebugString(_T("CreateFile fail!/r/n"));18         return FALSE;19     }20 21     hMapFile = CreateFileMapping(22         hFile,23         NULL,24         PAGE_READWRITE,25         0,26         dwSize,27         NULL28         );29     if( hMapFile == NULL )30     {31         OutputDebugString(_T("CreateFileMapping fail!/r/n"));32         CloseHandle( hFile );33         return FALSE;    34     }35 36     CloseHandle( hMapFile );37     CloseHandle( hFile );38 39     return TRUE;40 }