Click here to Skip to main content
13,138,411 members (52,680 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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() 
{
        // TODO: Add your control notification handler code here
        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; // FSCTL_SET_SPARSE=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,	//FILE_ATTRIBUTE_ARCHIVE FILE_ATTRIBUTE_NORMAL  FILE_FLAG_BACKUP_SEMANTICS
		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
Posted 11-Sep-12 1:40am
Updated 15-Sep-12 3:21am
v5
Comments
Richard MacCutchan 15-Sep-12 6:11am
   
Where is the code for creating the file?
terroblade 15-Sep-12 9:18am
   

thanks for you answer,the code is as follow:

void CFileCheckDlg::OnButtonCreate()
{
// TODO: Add your control notification handler code here
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; // FSCTL_SET_SPARSE=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);
}
Richard MacCutchan 15-Sep-12 9:47am
   
I think you may need to do something more when creating your file; see this section on MSDN.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

when i restarted my computer, the disk recoveried, i can`t understand.i need your help.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 15 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100