Situation :
1,creating a empty Sparse file on the given volume (system xp or server 2003) and specify the 10G size of the file.
2,Like reading a normal file ,codes as follow:
void CFileCheckDlg::OnButtonCreate()
{
UpdateData(TRUE);
HANDLE hFile = CreateFile(m_strSrcFile,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0);
if ( INVALID_HANDLE_VALUE == hFile)
{
LOGFILE("CreateFile failure,Error=%d",GetLastError());
return;
}
DWORD dwCtrlCode = 0x900c4; DWORD dwBytesReturned;
BOOL bRet = ::DeviceIoControl(hFile, dwCtrlCode, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
if (!bRet)
{
LOGFILE("sparse file set error!,dwError=%d",GetLastError());
return;
}
LARGE_INTEGER uli;
uli.QuadPart = m_dwSparseSize;
uli.QuadPart = uli.QuadPart<<20;
SetFilePointer(hFile,uli.LowPart,&uli.HighPart,FILE_BEGIN);
SetEndOfFile(hFile);
CloseHandle(hFile);
}
void CFileCheckDlg::ReadSparseFile()
{
HANDLE hFile = CreateFile(m_strFileName,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if ( INVALID_HANDLE_VALUE == hFile)
return FALSE;
ULARGE_INTEGER uli;
uli.LowPart = GetFileSize(hFile,&uli.HighPart);
DWORD dwSize = 1<<15;
DWORD dwCount = (DWORD)((uli.QuadPart/dwSize)+(uli.QuadPart%dwSize?1:0));
PBYTE pBuf = new BYTE[dwSize];
for (DWORD i=0; i<dwcount;> {
DWORD dwReal=0;
BOOL bRead = ReadFile(hFile,pBuf,dwSize,&dwReal,NULL);
if (!bRead)
{
CloseHandle(hFile);
return FALSE;
}
DWORD dwPos = (i+1)*100/dwCount;
m_cProgress.SetPos(dwPos);
}
delete []pBuf;
CloseHandle(hFile);
}
3,Sometimes, i find the free space of my disk decreases 10G,and even the ReadFile Function return error "disk full" ,
4,So far, i didn`t know why, i am glad to hear your tips. Thanks