views:

35

answers:

1

I'm testing a sparse file. But my test code doesn't work well.

HANDLE h = CreateFileW(L"D:\\sparse.test",
        GENERIC_READ|GENERIC_WRITE,
        FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
        0,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SPARSE_FILE,
        0);

DWORD d = GetFileAttributes(L"D:\\sparse.test");
// The function returns 32(FILE_ATTRIBUTE_ARCHIVE).
// Where is FILE_ATTRIBUTE_SPARSE_FILE flag?
// How do I make a sparse file.

DWORD written;
WriteFile(h, "aaa", 3, &written, 0);
SetFilePointer(h, 2*1024*1024*1023, 0, FILE_BEGIN);
SetEndOfFile(h);
WriteFile(h, "bbb", 3, &written, 0);
+1  A: 

You have to create a normal file, then use DeviceIoControl with FSCTL_SET_SPARSE to make it a sparse file.

Jerry Coffin
Oh, thanks. I'll try it. Isn't there a Win32 interface?
Benjamin
@Benjamin: I don't know of another way to do this job.
Jerry Coffin
Yes it works well. Thank you very much.
Benjamin