Creates or opens a file or I/O device. The most commonly used I/O devices are as follows: file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, and pipe. The function returns a handle that can be used to access the file or device for various types of I/O depending on the file or device and the flags and attributes specified.
To perform this operation as a transacted operation, which results in a handle that can be used for transacted I/O, use theCreateFileTransacted function.
主要用于打开一个文件或者是IO设备,最常用于打开一个IO设备,
函数原型:
HANDLE CreateFile(
LPCTSTR lpFileName, //指向文件名的指针
DWORD dwDesiredAccess, //访问模式(写/读)
DWORD dwShareMode, //共享模式
LPSECURITY_ATTRIBUTES lpSecurityAttributes, //指向安全属性的指针
DWORD dwCreationDisposition, //如何创建
DWORD dwFlagsAndAttributes, //文件属性
HANDLE hTemplateFile //用于复制文件句柄
);
参数列表
lpFileName String 要打开的文件的名字
dwDesiredAccess Long 如果为 GENERIC_READ 表示允许对设备进行读访问;如果为 GENERIC_WRITE 表示允许对设备进行写访问(可组合使用);如果为零,表示只允许获取与一个设备有关的信息
dwShareMode Long, 零表示不共享; FILE_SHARE_READ 和/或 FILE_SHARE_WRITE 表示允许对文件进行共享访问
lpSecurityAttributes SECURITY_ATTRIBUTES, 指向一个SECURITY_ATTRIBUTES结构的指针,定义了文件的安全特性(如果操作系统支持的话)
dwCreationDisposition Long,下述常数之一:
CREATE_NEW 创建文件;如文件存在则会出错
CREATE_ALWAYS 创建文件,会改写前一个文件
OPEN_EXISTING 文件必须已经存在。由设备提出要求
OPEN_ALWAYS 如文件不存在则创建它
TRUNCATE_EXISTING 讲现有文件缩短为零长度
dwFlagsAndAttributes Long, 一个或多个下述常数
FILE_ATTRIBUTE_ARCHIVE 标记归档属性
FILE_ATTRIBUTE_COMPRESSED 将文件标记为已压缩,或者标记为文件在目录中的默认压缩方式
FILE_ATTRIBUTE_NORMAL 默认属性
FILE_ATTRIBUTE_HIDDEN 隐藏文件或目录
FILE_ATTRIBUTE_READONLY 文件为只读
FILE_ATTRIBUTE_SYSTEM 文件为系统文件
FILE_FLAG_WRITE_THROUGH 操作系统不得推迟对文件的写操作
FILE_FLAG_OVERLAPPED 允许对文件进行重叠操作
FILE_FLAG_NO_BUFFERING 禁止对文件进行缓冲处理。文件只能写入磁盘卷的扇区块
FILE_FLAG_RANDOM_ACCESS 针对随机访问对文件缓冲进行优化
FILE_FLAG_SEQUENTIAL_SCAN 针对连续访问对文件缓冲进行优化
FILE_FLAG_DELETE_ON_CLOSE 关闭了上一次打开的句柄后,将文件删除。特别适合临时文件
也可在Windows NT下组合使用下述常数标记:
SECURITY_ANONYMOUS, SECURITY_IDENTIFICATION, SECURITY_IMPERSONATION, SECURITY_DELEGATION, SECURITY_CONTEXT_TRACKING, SECURITY_EFFECTIVE_ONLY
hTemplateFile Long, 如果不为零,则指定一个文件句柄。新文件将从这个文件中复制扩展属性
返回值
如执行成功,则返回文件句柄。
INVALID_HANDLE_VALUE表示出错,会设置GetLastError。即使函数成功,但若文件存在,且指定了CREATE_ALWAYS 或 OPEN_ALWAYS,GetLastError也会设为ERROR_ALREADY_EXISTS
实例:
1、直接在函数内指定文件地址:
- voidplayCEwav()
- {
- CHAR*pBuffer;
- DWORDRSize;
- intfileSize = 0;
- inti;
- HANDLE hOpenFile = (HANDLE)CreateFile(L"E:\\a.text", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
- if(hOpenFile == INVALID_HANDLE_VALUE)
- {
- hOpenFile = NULL;
- MessageBoxA(NULL, "Can not open the file", "Playwav", MB_OK);
- }
- fileSize = GetFileSize(hOpenFile, NULL);
- pBuffer = (CHAR*) malloc(fileSize);
- ReadFile(hOpenFile, pBuffer, fileSize, &RSize, NULL);
- //可将pBuffer显示在某区域或写入另一个文件来检查读出是否正确
- free(pBuffer);
- }
2、把文件地址通过参数传入:
- void playwav(TCHAR* path)
- {
- CHAR*pBuffer;
- DWORDRSize;
- intfileSize = 0;
- inti;
- TCHARszPath[100];
- memset(szPath, 0, sizeof(szPath));
- _tcscpy(szPath, path);
- HANDLE hOpenFile = (HANDLE)CreateFile(szPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
- if(hOpenFile == INVALID_HANDLE_VALUE)
- {
- hOpenFile = NULL;
- MessageBoxA(NULL, "Can not open the file", "Playwav", MB_OK);
- }
- fileSize = GetFileSize(hOpenFile, NULL);
- pBuffer = (CHAR*) malloc(fileSize);
- ReadFile(hOpenFile, pBuffer, fileSize, &RSize, NULL);
- free(pBuffer);
- }