C-fgets()-如果字符串较长,该怎么办? [英] C - fgets() - What if the string is longer?

查看:210
本文介绍了C-fgets()-如果字符串较长,该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含长度为10的字符串的文件:



abcdefghij



然后阅读我的文本文件逐行使用 fgets(),缓冲区大小为4。
我将不得不调用 fgets()循环以确保读取了整行。
但是,在第一次通话时,它将读取前3个字符(缓冲区大小-1)对吗?它是否还会在我的4个字符缓冲区的最后一个位置末尾添加一个空终止符,即使没有到达字符串的真正结尾,还是在缓冲区的末尾填充4个字符且不包含空终止符?这将导致无法调用 strlen()吗?



谢谢:)

解决方案

在文档中



逐字记录href = http://man7.org/linux/man-pages/man3/gets.3.html rel = nofollow> man fgets (我的斜体):


fgets()从流$中读取的字符数最多小于大小字符b $ b并将它们存储到s指向的缓冲区中。在 EOF 或换行符之后,读取停止。如果读取了换行符,它将存储在
缓冲区中。 在缓冲区中最后一个
字符之后存储一个终止的空字节('\0')。







HP-UX 11手册页上的逐字记录(我用斜体表示):


fgets()



将流中的字符读入
指向s的数组,直到读取n-1个字符为止,换行读取
字符并将其传输到s,或者遇到
文件结尾的情况。 然后字符串
以空字符终止。







来自 POSIX规范


fgets()函数应从流中将字节读取到s所指向的数组中,直到读取n-1个字节,或读取a并将其传送到,或者遇到文件结束条件。 然后该字符串以空字节结尾







至少不是来自MSDN


fgets函数从输入流参数中读取一个字符串并将其存储在str中。 fgets从当前流位置开始读取字符,包括第一个换行符(包括第一个换行符),直到流的末尾,或者直到读取的字符数等于n – 1,以先到者为准。存储在str中的结果将附加一个空字符。如果已读取,则换行符将包含在字符串中。



Let's say i have a file containing a string of length 10 :

abcdefghij

And read my text file line by line using fgets() with a buffer of size 4. I will have to call fgets() in a loop to make sure the whole line is read. But, at say the first call, it will read the first 3 characters (buffer size -1) right ? Will it also append a null terminating character at the last position of my 4 char buffer even if the real end of my string wasn't reached, or will fill it with 4 characters and no null terminating characters at the end of my buffer ? which would make a call to strlen() impossible ?

Thank you :)

解决方案

It is in the documentation.

Verbatim from man fgets (italics by me):

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.


Verbatim from the HP-UX 11 man page (italics by me):

fgets()

Reads characters from the stream into the array pointed to by s, until n-1 characters are read, a new-line character is read and transferred to s, or an end-of- file condition is encountered. The string is then terminated with a null character.


From the POSIX specs:

The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte.


Last not least from MSDN:

The fgets function reads a string from the input stream argument and stores it in str. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in str is appended with a null character. The newline character, if read, is included in the string.

这篇关于C-fgets()-如果字符串较长,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆