简体   繁体   English

ReadFile函数lpbuffer和字节以读取说明

[英]ReadFile function lpbuffer and bytes to read clarifcation

I'm pretty new to this so any clarification would be appreciated. 我对此很陌生,因此请您澄清一下。 When using the function ReadFile, how does the type of the lpBuffer interact with the parameter of "number of bytes to read"? 使用函数ReadFile时,lpBuffer的类型如何与“要读取的字节数”参数交互?

For instance what if you had an unsigned short MyShort[5] as lpBuffer, and then you set bytes to read as 2. Will all data be stored in MyShort[0]? 例如,如果您将无符号的短MyShort [5]作为lpBuffer,然后将字节设置为2,该怎么办?所有数据都将存储在MyShort [0]中吗? Or would the first byte go into MyShort[0] and the second byte go into MyShort[1]? 还是第一个字节进入MyShort [0],第二个字节进入MyShort [1]? What happens when you set bytes to read is increase say to 9? 当您将要设置的字节数设置为9时,会发生什么? Will 16bits go into MyShort[0] and then 16 more into MyShort[1] etc...? 将16bits放入MyShort [0],然后再将16bits放入MyShort [1]等吗?

Thanks 谢谢

lpBuffer is always treated as a pointer to an array of specified amount of bytes ( nNumberOfBytesToRead ). lpBuffer始终被视为指向指定字节数( nNumberOfBytesToRead )的数组的指针。 The amount of bytes actually read will be stored in the variable pointed to by lpNumberOfBytesRead parameter or as async (overlapped) result later. 实际读取的字节数将存储在lpNumberOfBytesRead参数指向的变量中,或稍后存储为异步(重叠)结果。 So in your case if you request to read 2 bytes it may either read two bytes storing both of them in MyShort[0] , or just a single byte stored in lower half of MyShort[0] or nothing at all. 因此,在您的情况下,如果您请求读取2个字节,则它可能会读取两个字节并将它们都存储在MyShort[0] ,或者只是一个字节存储在MyShort[0]下半部分中,或者根本不存储任何内容。 If you request to read 9 bytes then it will ready up to 9 bytes storing 2 + 2 + 2 + 2 + 1 bytes sequentially. 如果您请求读取9个字节,则它将准备最多 9个字节,依次存储2 + 2 + 2 + 2 + 1字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM