readfile函数使用问题(急)

来源:百度知道 编辑:UC知道 时间:2024/05/30 22:26:25
我是新手,官方文档也看的不是很明白,所以想请教达人可能很小白的问题:
readfile函数的读取是一次一个字节还是一次读完所有的?
比如说如下定义:
ClearCommError(hCom, &dwErrorFlags, &ComStat);
fState = ReadFile(hCom, buf, ComStat.cbInQue, &length, &ovRead);
这两句程序语句运行结束以后buf中是只有buf[0]有读取的数据还是全部欲读取数据都在buf中?
还有ComStat.cbInQue欲读取的字节个数系统是怎么知道的?
我还是有一点疑问 ,就是按照上面的例子,ComStat结构体是不是要提前设置?貌似咩有啊,而且传输的是不定长数据帧,也米有办法指定。如果是某个函数或者系统返回的值,那么某个函数或者系统是怎么知道欲传输的数据帧的长度的呢?按照文档ComStat.cbInQue是递减的,到传输完毕为0,所以一定有一个值;再或者说,传输不定长数据帧不能用这个?(完全晕了)
我是不是太白了一点?不好意思,我完全不是学这个的,而且时间太紧了。PS:我会加分的。

This 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.

"After the read operation has been completed,"由此句应该看出是一次将要读的字节读完。
想想就知道了,要是一个一个字节读,要是文件稍微大点,这要读到猴年马月阿。

BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);

lpBuffer
[out] Pointer to the buffer that receives the data read from the file.
参考上述说明,可以知道lpBuffer是指向读取的全部数据的第一个位置;
换句话说就是你的“读取数据都在buf中”的意思

nNumberOfBytesToRead
[in] Number of bytes to be read from the file.
这个参数是in,就是要输入的,是你指定ReadFile函数要读取的byte数,要你指定!!!