根据 http://linux.die.net/man/3/sprintf和 http://www.cplusplus.com/reference/cstdio/sprintf/ sprintf()
和 family 返回成功写入的字符数。失败时,返回一个负值。我假设如果格式字符串格式错误,则可能会发生错误,因此负返回值可能表示 malloc()
错误之外的其他内容。 errno
是否设置为指示错误是什么?
最佳答案
C++ 遵循 C 并且 C 在 sprintf()
和系列的描述中不需要或提及 errno
(尽管对于某些格式说明符,这些函数被定义为调用mbrtowc()
,可能会在errno
中设置EILSEQ
)
POSIX 要求设置错误号:
If an output error was encountered, these functions shall return a negative value and set
errno
to indicate the error.
明确提及 EILSEQ、EINVAL、EBADF、ENOMEM、EOVERFLOW:http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
关于c - sprintf() 负返回值和errno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14778123/