gpt4 book ai didi

c - 为什么为了使用 fgets 读取多行文本,我们需要设置 fgets()!=NULL?

转载 作者:行者123 更新时间:2023-11-30 14:42:45 28 4
gpt4 key购买 nike

如果我想使用 fgets 从文本中读取多行,根据我的教科书,我会这样做:

 char str[53];
...
while(fgets(str, max, f)!=NULL){
...
}

如果str只是一行n个字符的字符串,程序如何读取第二行?根据文档,循环到达行尾时不应该停止吗?

fgets Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

最佳答案

根据文档:

On success, the function returns str. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).

str 是您指定为第一个参数的缓冲区。如果fgets读取完毕,则返回NULL。

关于c - 为什么为了使用 fgets 读取多行文本,我们需要设置 fgets()!=NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54331673/

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