Click here to Skip to main content
15,892,674 members

when reading a sparse file, the free space of disk decreases.

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:
C++
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++

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

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.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900