在 Linux 与 Windows 中读取系统

read system in Linux vs Windows

在Linux中使用read()与在Windows中使用有什么区别吗?

有没有可能在Windows中,它通常读得比我要求的少,而在Linux中,它通常读得和我要求的一样多?

read 不是标准的 c 函数。从历史上看,它是一个 posix 系统调用,因此,根本不需要 windows(假设 windows 表示 MSVC)来实现它。尽管如此,他们还是尝试了。我们可以比较两种实现方式:

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.

因此,您应该期望这两种实现都 return 少于请求的字节数。此外,在文本模式下读取文件时也有明显的区别。