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

打开设备: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.

Note  Delete 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写入的数据,会覆盖掉原本的数据而不是插入数据。

你可能感兴趣的:(Chapter10-“I/O设备的同步和异步”之打开和关闭设备)