gpt4 book ai didi

c - `getchar()` 返回错误的具体情况是什么?

转载 作者:行者123 更新时间:2023-12-01 23:28:17 27 4
gpt4 key购买 nike

所以我知道 getchar() 在输入结束或发生错误时返回 EOF。我也知道我可以通过 ferror(stdin)feof(stdin) 来检查哪些情况发生了。我想知道特别是在什么情况下会发生错误。

我检查了这两个函数的手册页,但那里什么也没有。

最佳答案

getchar() 可以为多个系统特定的 I/O 错误返回 EOFgetchar() 被定义为等效于 getc(stdin),它本身等效于 fgetc(stdin),除了它可能是作为宏实现。以下是来自 linux man page 的 linux 系统的可能原因列表。 :

RETURN VALUE

Upon successful completion, fgetc() shall return the next bytefrom the input stream pointed to by stream. If the end-of-fileindicator for the stream is set, or if the stream is at end-of-file, the end-of-file indicator for the stream shall be set andfgetc() shall return EOF. If a read error occurs, the errorindicator for the stream shall be set, fgetc() shall return EOF,and shall set errno to indicate the error.

ERRORS

The fgetc() function shall fail if data needs to be read and:

EAGAIN The O_NONBLOCK flag is set for the file descriptorunderlying stream and the thread would be delayed in thefgetc() operation.

EBADF The file descriptor underlying stream is not a valid filedescriptor open for reading.

EINTR The read operation was terminated due to the receipt of asignal, and no data was transferred.

EIO A physical I/O error has occurred, or the process is in abackground process group attempting to read from itscontrolling terminal, and either the calling thread isblocking SIGTTIN or the process is ignoring SIGTTIN or theprocess group of the process is orphaned. This error mayalso be generated for implementation-defined reasons.

EOVERFLOWThe file is a regular file and an attempt was made to readat or beyond the offset maximum associated with thecorresponding stream.

The fgetc() function may fail if:

ENOMEM Insufficient storage space is available.

ENXIO A request was made of a nonexistent device, or the requestwas outside the capabilities of the device.

关于c - `getchar()` 返回错误的具体情况是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66814028/

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