稀疏文件的问题

halod 2004-01-05 10:23:18
建立一个稀疏文件,CreateFileMapping()设为1M,写入一个字符,为什么磁盘空间的占用还是1M,源程序如下:
#define _WIN32_WINNT 0x0500
#define dwStreamMaxSize (1*1024*1024)
#include <windows.h>
#include <Winioctl.h>
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
HANDLE hfile=CreateFile("E://mmfsparse.dat",GENERIC_READ
|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hfile==INVALID_HANDLE_VALUE){
cout<<"cannot CREATE file"<<endl;
CloseHandle(hfile);
getchar();
return false;
}
DWORD dw;
DeviceIoControl(hfile,FSCTL_SET_SPARSE,NULL,0,NULL,0,&dw,NULL);
HANDLE hfilemap=CreateFileMapping(hfile,NULL,PAGE_READWRITE,
0,dwStreamMaxSize,NULL);
PVOID hvfile=MapViewOfFile(hfilemap,FILE_MAP_READ|FILE_MAP_WRITE,0,0,0);
((PBYTE)hvfile)[1*1024]='a';
UnmapViewOfFile(hvfile);
CloseHandle(hfilemap);
CloseHandle(hfile);
getchar();
return 1;
}
...全文
347 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
halod 2004-01-12
  • 打赏
  • 举报
回复
谢谢大家来讨论,这个问题可能暂时没有确定的答案,如果下次有类似的
问题,希望一起再讨论。
chehw 2004-01-11
  • 打赏
  • 举报
回复
使用DeviceIoControl(),不需要在文件属性中设置压缩。
但文件占用空间不是4K, 在我这儿显示是64K。原理不清楚。
halod 2004-01-11
  • 打赏
  • 举报
回复
何况就算不使用DeviceIoControl(),仅仅在文件属性中设置压缩,
文件占用空间也是4k
halod 2004-01-09
  • 打赏
  • 举报
回复
因为我不可能每次都手工为该文件选择属性
halod 2004-01-09
  • 打赏
  • 举报
回复
如果在文件的高级属性中选压缩,则占用磁盘空间为4k,但我觉得这样
不符合DeviceIoControl()函数的原意,大家怎么看?
bluebohe 2004-01-09
  • 打赏
  • 举报
回复
你把生成的文件放到已经经过压缩的文件夹或者驱动器里
halod 2004-01-09
  • 打赏
  • 举报
回复
待俺到XP下试试看
halod 2004-01-09
  • 打赏
  • 举报
回复
难道win2000的ntfs不是ntfs5
chehw 2004-01-08
  • 打赏
  • 举报
回复
你的程序在我这运行没有问题。
文件大小1M,
磁盘占用空间64K。

注意:稀疏文件大小的仍是1M,不会改变。通过压缩后在磁盘所占空间会大幅减少。
bluebohe 2004-01-08
  • 打赏
  • 举报
回复
内存影像文件就是把一块内存保存到文件,该内存的长度由dwMaximumSizeLow 和dwMaximumSizeHigh指定,他并不知道你到底往文件所在内存写了些什么,即使仅仅写了一个字符,也一样是那么大的内存,如果你不知道存储多少数据的话,到不如用普通文件,这样的话可以根据写入内容多少来增减文件大小
你看一下MSND对CreateFileMapping的解释:
CreateFileMapping

dwMaximumSizeLow
[in] Low-order DWORD of the maximum size of the file-mapping object. If this parameter and dwMaximumSizeHigh are zero, the maximum size of the file-mapping object is equal to the current size of the file identified by hFile.
An attempt to map a file with a length of zero in this manner fails with an error code of ERROR_FILE_INVALID. Applications should test for files with a length of zero and reject such files.

Return Values
If the function succeeds, the return value is a handle to the file-mapping object. If the object existed before the function call, the function returns a handle to the existing object (with its current size, not the specified size) and GetLastError returns ERROR_ALREADY_EXISTS.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks
After a file-mapping object has been created, the size of the file must not exceed the size of the file-mapping object; if it does, not all of the file's contents will be available for sharing.

If an application specifies a size for the file-mapping object that is larger than the size of the actual named file on disk, the file on disk is grown to match the specified size of the file-mapping object. If the file cannot be grown, this results in a failure to create the file-mapping object. GetLastError will return ERROR_DISK_FULL.

The handle that CreateFileMapping returns has full access to the new file-mapping object. It can be used with any function that requires a handle to a file-mapping object. File-mapping objects can be shared either through process creation, through handle duplication, or by name. For information on duplicating handles, see DuplicateHandle. For information on opening a file-mapping object by name, see OpenFileMapping.

Windows 95/98/Me: File handles that have been used to create file-mapping objects must not be used in subsequent calls to file I/O functions, such as ReadFile and WriteFile. In general, if a file handle has been used in a successful call to the CreateFileMapping function, do not use that handle unless you first close the corresponding file-mapping object.

Creating a file-mapping object creates the potential for mapping a view of the file but does not map the view. The MapViewOfFile and MapViewOfFileEx functions map a view of a file into a process's address space.

With one important exception, file views derived from a single file-mapping object are coherent, or identical, at a given time. If multiple processes have handles of the same file-mapping object, they see a coherent view of the data when they map a view of the file.

The exception has to do with remote files. Although CreateFileMapping works with remote files, it does not keep them coherent. For example, if two computers both map a file as writable, and both change the same page, each computer will only see its own writes to the page. When the data gets updated on the disk, it is not merged.

A mapped file and a file accessed by means of the input and output (I/O) functions (ReadFile and WriteFile) are not necessarily coherent.

To fully close a file-mapping object, an application must unmap all mapped views of the file-mapping object by calling UnmapViewOfFile, and close the file-mapping object handle by calling CloseHandle. The order in which these functions are called does not matter. The call to UnmapViewOfFile is necessary because mapped views of a file-mapping object maintain internal open handles to the object, and a file-mapping object will not close until all open handles to it are closed.

Note Terminal Services sessions can use shared memory blocks to transfer data between processes spawned by those sessions. If you do this, keep in mind that shared memory cannot be used in situations where both of the following conditions exist:

All of the processes using the shared memory block were not spawned by one session.
All of the sessions share the same user logon credential.
Note To guard against an access violation, use structured exception handling to protect any code that writes to or reads from a memory mapped view. For more information, see Reading and Writing.
halod 2004-01-08
  • 打赏
  • 举报
回复
看来100分没人拿了,up
halod 2004-01-07
  • 打赏
  • 举报
回复
是在win2000下,NTFS格式,应该是NTFS5吧
101monster 2004-01-06
  • 打赏
  • 举报
回复
呵呵,UP!
chehw 2004-01-06
  • 打赏
  • 举报
回复
你的文件系统是 NTFS5.0以上吗?
看一下DeviceIoControl的返回值,如果为0,GetLastError()看看是什么错误。
halod 2004-01-06
  • 打赏
  • 举报
回复
大家帮帮忙啊,有没有人能搞定
bluebohe 2004-01-05
  • 打赏
  • 举报
回复
内存映射文件就是相当于把一段指定大小的内存内容写入到文件中,当然是这样
halod 2004-01-05
  • 打赏
  • 举报
回复
按你所说,如果用
HANDLE hfilemap=CreateFileMapping(hfile,NULL,PAGE_READWRITE,
0,0,NULL);
那么还有什么必要使用稀疏文件呢?
稀疏文件就是不知道将要存储多少数据的文件,在我的程序中,我只
知道1M空间肯定够用,但是申请了1M的空间肯定有浪费,为了减少浪
费,所以我才使用稀疏文件。
照你所说,正确的做法应该怎样?
请教,谢谢!!
bluebohe 2004-01-05
  • 打赏
  • 举报
回复
你用这种方式肯定结果就是一兆,除非你是用普通方式,稀疏矩阵仅仅在内存中存在,序列化到文件中时以紧凑的方式保存
halod 2004-01-05
  • 打赏
  • 举报
回复
但是只有一个字符的文件却要占用1M,这显然不合理,所以要用稀疏文件

2,643

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧