简体   繁体   中英

read system in Linux vs Windows

Is there any difference between using read() in Linux than in Windows?

Is it possible that in Windows, it will usually read less than I request, and in Linux it usually reads as much as I request?

read isn't a standard c function. Historically it is a posix syscall, and as such, windows (assuming windows means MSVC) isn't required to implement it at all. Still, they tried. And we can compare the two implementations:

linux:

http://man7.org/linux/man-pages/man2/read.2.html

On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of- file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. See also NOTES.

windows:

https://msdn.microsoft.com/en-us/library/ms235412.aspx

https://msdn.microsoft.com/en-us/library/wyssk1bs.aspx

_read returns the number of bytes read, which might be less than count if there are fewer than count bytes left in the file or if the file was opened in text mode, in which case each carriage return–line feed (CR-LF) pair is replaced with a single linefeed character. Only the single linefeed character is counted in the return value. The replacement does not affect the file pointer.

So you should expect both implementations to return less than the requested number of bytes. Futhermore, there is a clear difference when reading files in text mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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