The getline() Function

The latest and most trendy function for reading a string of text is getline(). It’s a new C library function, having appeared around 2010 or so.

You might not have heard of the getline() function, and a few C programmers avoid it because it uses — brace yourself — pointers! Even so, it’s a good line-input function, and something you should be familiar with, even if you don’t plan on using it.

Here’s a typical getline() statement:

getline(&buffer,&size,stdin);

The getline() function is prototyped in the stdio.h header file. Here are the three arguments:

&buffer is the address of the first character position where the input string will be stored. It’s not the base address of the buffer, but of the first character in the buffer. This pointer type (a pointer-pointer or the ** thing) causes massive confusion.

&size is the address of the variable that holds the size of the input buffer, another pointer.

stdin is the input file handle. So you could use getline() to read a line of text from a file, but when stdin is specified, standard input is read.

Here’s a sample program:

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

int input(char *s,int length);

int main()
{
    char *buffer;
    size_t bufsize = 32;
    size_t characters;

    buffer = (char *)malloc(bufsize * sizeof(char));
    if( buffer == NULL)
    {
        perror("Unable to allocate buffer");
        exit(1);
    }

    printf("Type something: ");
    characters = getline(&buffer,&bufsize,stdin);
    printf("%zu characters were read.\n",characters);
    printf("You typed: '%s'\n",buffer);

    return(0);
}

The string input is stored at the memory location referenced by pointer buffer, declared at Line 8.

Lines 9 and 10 use the size_t variable type, which is a special type of integer. That’s required by the getline() function. The buffer size is 32 characters, which is set at Line 9. It must be referenced as a pointer, not a literal value.

In this code, 32 bytes of storage are assigned to memory location buffer via the malloc() function at Line 12. Lines 13 through 17 handle the (rare) condition when memory isn’t available. Odds are low that would happen in this program, but it’s good programming practice to check.

The getline() function debuts at Line 20. It uses the address of buffer, bufsize, and then stdin for standard input.

Because variable characters is a size_t variable type, the %zu placeholder is used in the printf() function at Line 21.

Here’s a sample run:

Type something: Today is January 3, 2015
25 characters were read.
You typed: 'Today is January 3, 2015
'

Notice anything annoying with the output?

Yep, as with the fgets() function, getline() reads and stores the newline character as part of the string. So if the pointer thing bothers you, just use fgets() instead.

Finally, if you’re curious, you can use getline() with array notation for the storage buffer. It ain’t pretty, but it works:

#include <stdio.h>

int input(char *s,int length);

int main()
{
    char buffer[32];
    char *b = buffer;
    size_t bufsize = 32;
    size_t characters;

    printf("Type something: ");
    characters = getline(&b,&bufsize,stdin);
    printf("%zu characters were read.\n",characters);
    printf("You typed: '%s'\n",buffer);

    return(0);
}

To get the ** type of pointer from array notation, you need to declare a pointer variable and assign it to the array, which is done above in Line 8. You can then use that pointer with the ampersand in the getline() function, shown above at Line 13.