gpt4 book ai didi

c - gets() 函数和输入中的 '\0' 零字节

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:42 25 4
gpt4 key购买 nike

如果从文件中读取零字节 ('\0'),C 语言(例如 glibc)的 gets() 函数会停止吗?

快速测试:echo -ne 'AB\0CDE'

谢谢。

PS 这个问题来自这个问题的评论:return to libc - problem

PPS gets 函数是危险的,但这是关于这个函数本身的问题,而不是任何人是否应该使用它的问题。

最佳答案

gets() 的行为是在遇到换行符或遇到 EOF 时停止。它不关心它是否读取 \0 字节。

C99 标准,7.19.7.7

Synopsis

   #include <stdio.h>

char *gets(char *s);

Description

The gets function reads characters from the input stream pointed to by stdin, into the array pointed to by s, until end-of-file is encountered or a new-line character is read. Any new-line character is discarded, and a null character is written immediately after the last character read into the array.

来自 GNU libc 文档:http://www.gnu.org/software/libc/manual/html_node/Line-Input.html#Line-Input

— Deprecated function: char * gets (char *s)

The function gets reads characters from the stream stdin up to the next newline character, and stores them in the string s. The newline character is discarded (note that this differs from the behavior of fgets, which copies the newline character into the string). If gets encounters a read error or end-of-file, it returns a null pointer; otherwise it returns s.

关于c - gets() 函数和输入中的 '\0' 零字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5068278/

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