Chapter10-“I/O设备的同步和异步”之打开和关闭设备

本文详细介绍了Windows API中的CreateFile函数,不仅用于打开文件,还能用于打开多种I/O设备。包括如何设置参数如文件名、访问权限、共享模式、创建方式等,并提供了一个在文件尾部添加数据的简单示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打开设备:CreateFile函数

函数原型:
HANDLE WINAPI CreateFile(
__in LPCTSTR lpFileName,
__in DWORD dwDesiredAccess,
__in DWORD dwShareMode,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__in DWORD dwCreationDisposition,
__in DWORD dwFlagsAndAttributes,
__in_opt HANDLE hTemplateFile);

首先不要被它的名字所迷惑,这个函数不仅可以打开文件(Files),它还可以打开不少的I/O设备(如串口等)。下面再介绍参数用法:

1)lpFileName参数:指定需要打开或创建的文件名或设备名。

2)dwDesiredAccess参数:指定请求的权限,综合来说就是read/write/both/neither.详情参考:GenericAccess Rights,FileSecurity and Access Rights, FileAccess Rights Constants, and ACCESS_MASK.

3)dwShareMode参数:指定打开设备的共享模式(read, write, both, delete, all of these, or none),下面的表格列出了其值域:

Value

Meaning

0

0x00000000

Prevents other processes from opening a file or device if they request delete, read, or write access.

FILE_SHARE_DELETE

0x00000004

Enables subsequent open operations on a file or device to request delete access.
Otherwise, other processes cannot open the file or device if they request delete access.
If this flag is not specified, but the file or device has been opened for delete access, the function fails.

NoteDelete access allows both delete and rename operations.

FILE_SHARE_READ

0x00000001

Enables subsequent open operations on a file or device to request read access.
Otherwise, other processes cannot open the file or device if they request read access.
If this flag is not specified, but the file or device has been opened for read access, the function fails.

FILE_SHARE_WRITE

0x00000002

Enables subsequent open operations on a file or device to request write access.
Otherwise, other processes cannot open the file or device if they request write access.
If this flag is not specified, but the file or device has been opened for write access or has a file mapping with write access, the function fails.

4)lpSecurityAttributes参数:指定一些相关的安全属性。

5)dwCreationDisposition参数:指定一些创建时处理操作,除了打开文件(files)外,对于其他设备一般使用OPEN_EXISTING作为参数值。其值必须是一下某个值,且不能结合使用。

CREATE_ALWAYS

2

Creates a new file, always.

If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set toERROR_ALREADY_EXISTS (183).

If the specified file does not exist and is a valid path, a new file is created, the function succeeds, and the last-error code is set to zero.

CREATE_NEW

1

Creates a new file, only if it does not already exist.

If the specified file exists, the function fails and the last-error code is set toERROR_FILE_EXISTS (80).

If the specified file does not exist and is a valid path to a writable location, a new file is created.

OPEN_ALWAYS

4

Opens a file, always.

If the specified file exists, the function succeeds and the last-error code is set toERROR_ALREADY_EXISTS (183).

If the specified file does not exist and is a valid path to a writable location, the function creates a file and the last-error code is set to zero.

OPEN_EXISTING

3

Opens a file or device, only if it exists.

If the specified file or device does not exist, the function fails and the last-error code is set toERROR_FILE_NOT_FOUND (2).

TRUNCATE_EXISTING

5

Opens a file and truncates it so that its size is zero bytes, only if it exists.

If the specified file does not exist, the function fails and the last-error code is set toERROR_FILE_NOT_FOUND (2).

The calling process must open the file with the GENERIC_WRITE bit set as part of the dwDesiredAccess parameter.

6)dwFlagsAndAttributes参数:最常用的值就是FILE_ATTRIBUTE_NORMAL,其他可用值请参考上面函数的MSDN链接。

7)hTemplateFile参数:指定拥有GENERIC_READ访问权限的模板文件的可用句柄,这个模板文件提供了要创建的文件属性和扩展属性。该参数可以为NULL,如果是调用CreateFile去打开一个已经存在的文件,这个参数被忽略。

关闭设备——CloseHandle函数

BOOL WINAPI CloseHandle( __in HANDLEhObject );
这个函数很简单,将CreateFile函数返回的句柄作为参数传入,就可关闭相应的设备了。


一个在文件尾添加数据的简单示例:

int AppendData()
{
 HANDLE h = CreateFile(TEXT("test.txt"), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 if (INVALID_HANDLE_VALUE == h) 
 { 
  cout<<"打开文件失败!"<<endl;
  return -1;
 } 
 else 
 { 
  TCHAR str[]=TEXT("fuck you"); 
  DWORD lenWritten; 
  SetFilePointer(h, 0, NULL, FILE_END);
  WriteFile(h, str, sizeof(str), &lenWritten, NULL); 
  CloseHandle(h); 
 }

 return 0; 
}
注意的是:需要通过SetFilePointer函数来将写入数据的指针定位于文件尾部,然后在尾部添加数据。
WriteFile是不可以直接插入数据的,通过SetFilePoint函数定位指针再利用WriteFile写入的数据,会覆盖掉原本的数据而不是插入数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值