关闭

File Functions--文件函数

63人阅读 评论(0) 收藏 举报
分类:

CreateFile

The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object(并返回可以访问的句柄):

  • files 文件
  • pipes 管道
  • mailslots 邮槽
  • communications resources 通信资源
  • disk devices (Windows NT only) 磁盘驱动器
  • consoles 控制台
  • directories (open only)目录(只读打开)

原型:

HANDLE CreateFile(

LPCTSTR lpFileName, //指向文件名的指针

DWORD dwDesiredAccess, //访问模式(写/读)

DWORD dwShareMode, //共享模式

LPSECURITY_ATTRIBUTES lpSecurityAttributes, //指向安全属性的指针

DWORD dwCreationDisposition, //如何创建

DWORD dwFlagsAndAttributes, //文件属性

HANDLE hTemplateFile //用于复制文件句柄

);

ReadFile

The ReadFile function reads data from a file, starting at the position indicated by the file pointer(从文件指针指向的位置开始将数据读出到一个文件中).
After the read operation has been completed(读操作结束后), the file pointer is adjusted(调整) by the number of bytes actually(实际) read(文件指针增加实际读取的自己数),
If the file handle is created for overlapped input and output (I/O)(), the application must adjust the position of the file pointer after the read operation(如果文件句柄为异步创建输入和输出(I / O),读操作后应用程序必须调整文件指针的位置后读操作。).

原型:
BOOL ReadFile(
HANDLE hFile, //文件的句柄
LPVOID lpBuffer, //用于保存读入数据的一个缓冲区
DWORD nNumberOfBytesToRead, //要读入的字节数
LPDWORD lpNumberOfBytesRead, //指向实际读取字节数的指针
LPOVERLAPPED lpOverlapped //如文件打开时指定了FILE_FLAG_OVERLAPPED,那么必须用这个参数引用一个特殊的结构。该结构定义了一次异步读取操作。否则,应将这个参数设为NULL 
);

返回值:
调用成功,返回非0;
调用不成功,返回为0;
会设置GetLastError。如启动的是一次异步读操作,则函数会返回零值,并将ERROR_IO_PENDING设置成GetLastError的结果。如结果不是零值,但读入的字节数小于nNumberOfBytesToRead参数指定的值,表明早已抵达了文件的结尾。

WriteFile

The WriteFile function writes data to a file and is designed for both synchronous and asynchronous operation(从文件指针指向的位置开始将数据写入到一个文件中, 且支持同步和异步操作,).
The function starts writing data to the file at the position indicated(指定) by the file pointer(从文件指针指向的位置开始写数据). After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written, except when the file is opened with FILE_FLAG_OVERLAPPED.(如果文件打开方式没有指明FILE_FLAG_OVERLAPPED的话,在写操作结束后,文件指针被增加实际读取的字节大小)
If the file handle was created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the write operation is finished. (如果文件要交互使用的话,在写操作结束后应用程序必须要记得调整文件指针)

原型:
BOOL WriteFile(

HANDLE hFile, // 文件句柄

LPCVOID lpBuffer, // 数据缓存区指针

DWORD nNumberOfBytesToWrite, // 你要写的字节数

LPDWORD lpNumberOfBytesWritten, // 用于保存实际写入字节数的存储区域的指针

LPOVERLAPPED lpOverlapped // OVERLAPPED结构体指针

);

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4175次
    • 积分:333
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:7篇
    • 译文:13篇
    • 评论:0条
    文章分类