gpt4 book ai didi

c - 在 Linux 与 Windows 中读取系统

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:51:00 24 4
gpt4 key购买 nike

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

window :

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.

因此,您应该期望这两种实现都返回少于请求的字节数。此外,在文本模式下读取文件时存在明显差异。

关于c - 在 Linux 与 Windows 中读取系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43676161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com