Click here to Skip to main content

terroblade asked:

Open original thread
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
Tags: C++

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2014
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