简体   繁体   English

fgets读取的最大大小

[英]fgets maximum size read

Using fgets to input a string, I have doubts related to length of string read. 使用fgets输入字符串,我对字符串读取的长度有疑问。

For example, consider the following program. 例如,考虑以下程序。

char str[50];
int i;
int len;
printf("Enter name:\n");
fgets(str,11,stdin);
len = strlen(str);
printf("len :  %d\n",len);
  1. If I enter 123456789 , strlen gives 10. 如果我输入123456789strlen给出10。

  2. If I enter 1234567890 , strlen given is 10 again ?? 如果我输入1234567890 ,则给定的strlen又是10?

I think strlen is considering newline also for string length. 我认为strlen也在考虑换行符来获取字符串长度。 Am I correct? 我对么? (I understand fgets using newline as part of string) (我了解使用换行符作为字符串一部分的fgets

What's wrong with (2) where I enter exactly 10 characters, Here string length should be 11 right? (2)到底输入了10个字符有什么问题,这里的字符串长度应该是11吗? 10 + 1 (for newline) = 11 10 +1(换行符)= 11

fgets reads at most 1 fewer characters than the length argument given, and does retain the newline as part of the input - so long as the newline is part of the first (length - 1) characters. fgets读取的字符数最多比给定的length参数少1个字符,并且确实将换行符保留为输入的一部分-只要换行符是第一个(长度-1)个字符的一部分。

So in your first case, assuming that 123456789 is followed by a newline, fgets has read 9 characters including the newline, yielding a string length of 10; 因此,在第一种情况下,假设123456789后跟换行符,则fgets读取了包括换行符在内的9个字符,字符串长度为10; in your second case, fgets will stop after reading the 10 characters 1234567890 , yielding a string length of 10. 在第二种情况下, fgets将在读取10个字符1234567890后停止,产生的字符串长度为10。

Here is an example: 这是一个例子:

#include <stdio.h>
#include <string.h>

#define MAX_DIGITS 5

int
main ()
{
  char buf[80];
  char *s = NULL;
  printf ("\n>>Enter string, %d digits or less: ", MAX_DIGITS);
  s = fgets (buf, MAX_DIGITS+1, stdin);
  printf ("strlen(buf)=%d, buf=%s, strlen(s)=%d, s=%s\n",
    strlen(buf), buf, strlen(s), s);
  return 0;
}

Sample output, with "MAX_DIGITS" and "MAX_DIGITS + 1": 示例输出,带有“ MAX_DIGITS”和“ MAX_DIGITS + 1”:

>>Enter string, 5 digits or less: 1
strlen(buf)=2, buf=1
, strlen(s)=2, s=1
.

>>Enter string, 5 digits or less: 12
strlen(buf)=3, buf=12
, strlen(s)=3, s=12
.

>>Enter string, 5 digits or less: 123
strlen(buf)=4, buf=123
, strlen(s)=4, s=123
.

>>Enter string, 5 digits or less: 1234
strlen(buf)=5, buf=1234
, strlen(s)=5, s=1234
.

>>Enter string, 5 digits or less: 12345
strlen(buf)=5, buf=12345, strlen(s)=5, s=12345.

>>Enter string, 5 digits or less: 123456
strlen(buf)=5, buf=12345, strlen(s)=5, s=12345.

You'll notice: 您会注意到:

  1. The return buffer retains the "\\n" as long as the #/digits are < MAX_DIGITS. 只要#/数字<MAX_DIGITS,返回缓冲区将保留“ \\ n”。

  2. The "\\n" is REMOVED when #/digits >= MAX_DIGITS. #/位数> = MAX_DIGITS时,将删除“ \\ n”。

  3. Your buffer must accomodate MAX_DIGITS+1 您的缓冲区必须容纳MAX_DIGITS + 1

Actually fgets requires a size specification (in your case 11 ) account for the \\0 at the end of a string. 实际上, fgets需要一个size说明(在您的情况下为11 )在字符串末尾占\\0 From the fgets man page: fgets手册页:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. fgets()最多从流中读取小于大小的字符,并将它们存储到s所指向的缓冲区中。 Reading stops after an EOF or a newline. 在EOF或换行符之后停止读取。 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. 终止的空字节(\\ 0)存储在缓冲区中的最后一个字符之后。

So we know that reading stops at \\n , when you enter 123456789\\n . 因此我们知道,当您输入123456789\\n时,读取会在\\n停止。 However when you enter 1234567890\\n , fgets() processes the input but it only takes the 10 characters and ignores everything else afterwards. 但是,当您输入1234567890\\nfgets()处理输入,但仅使用10个字符,之后将忽略所有其他内容。

Any extra input of your string and your string will at size-1 of fget() with last character as \\0 so the output remains the same. 字符串和字符串的任何其他输入都将以fget() size-1 ,最后一个字符为\\0因此输出保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM