串口之ReadFile WriteFile函数详解

本文详细解析了Windows API中的ReadFile和WriteFile函数,特别是WriteFile的异步操作特性。当使用FILE_FLAG_OVERLAPPED打开文件时,需要考虑文件指针的调整。同时,文章提供了一个WriteFile函数的应用实例。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

                BOOL ReadFile(

  HANDLE hFile, //文件的句柄

  LPVOID lpBuffer, //用于保存读入数据的一个缓冲区

  DWORD nNumberOfBytesToRead, //要读入的字符数

  LPDWORD lpNumberOfBytesRead, //指向实际读取字节数的指针

  LPOVERLAPPED lpOverlapped //如文件打开时指定了FILE_FLAG_OVERLAPPED,那么必须,用这个参数引用一个特殊的结构。该结构定义了一次异步读取操作。否则,应将这个参数设为NULL

  );

功能说明

  从文件指针指向的位置开始将数据读出到一个文件中, 且支持同步和异步操作,

  如果文件打开方式没有指明FILE_FLAG_OVERLAPPED的话,当程序调用成功时,它将实际读出文件的字节数保存到lpNumberOfBytesRead指明的地址空间中。

  如果文件要交互使用的话,当函数调用完毕时要记得调整文件指针。

  从文件中读出数据。与lread函数相比,这个函数要明显灵活的多。该函数能够操作通信设备、管道、套接字以及邮槽。

参数说明

  HANDLE hFile, 需要写入数据的文件指针,这个指针指向的文件必须是GENERIC_READaccess 访问属性的文件。

  LPOVERLAPPED lpOverlapped OVERLAPPED结构体指针,如果文件是以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. 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.

This function is designed for both synchronous and asynchronous operation. TheWriteFileEx function is designed solely for asynchronous operation. It lets an application perform other processing during a file write operation.

BOOL WriteFile(  HANDLE hFile,                    // handle to file  LPCVOID 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值