Search code examples
cstdinfgets

Using fgets to read a line from stdin (terminal), the maximum length is 1024?


Update:
OSX El Capitan, Xcode 7.3

Input:

0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890001

Here is my code:

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

#define BUFFERSIZE 2048

int 
main()
{
    char buffer[BUFFERSIZE];
    printf("Enter a message: \n");
    while (fgets( buffer, BUFFERSIZE, stdin) != NULL)
    {
        printf("%s\n", buffer);
    }

    return 0;
}

compile and run it in terminal.

./test

then input one line characters and length is 1024. It doesn't work; it cannot print the buffer. When I input 1023 characters, it will print the 1023 characters. It can print more than 1024 characters which fgets reads from a local open file.

So, with standard input from a terminal, the max length is 1024, even though <syslimits.h> shows ARG_MAX is (256 * 1024).

What is wrong with my code?

I have some references:


Solution

  • There's nothing wrong with the code per se. The problem is the terminal driver which has a limit of 1 KiB in its buffer, so you can't input more than the 1023 characters plus a newline. Most systems have a similar limit. Historically, the limit was a lot smaller, like 256 bytes.