gets

get a string of characters from a file

Synopsis:

#include <stdio.h>
char *gets( char *buf );
char *_ugets( _unichar *buf );

Description:

The gets() function gets a string of characters from the file designated by stdin, and stores them in the array pointed to by buf until end-of-file is encountered or a new-line character is read. Any new-line character is discarded, and a null character is placed immediately after the last character read into the array.

It is recommended that fgets() be used instead of gets() because data beyond the array buf will be destroyed if a new-line character is not read from the input stream stdin before the end of the array buf is reached.

A common programming error is to assume the presence of a new-line character in every string that is read into the array. A new-line character might not appear as the last character in a file, just before end-of-file.

Returns:

The gets() function returns buf if successful. NULL is returned if end-of-file is encountered, or if a read error occurs. When an error has occurred, errno contains a value that indicates the type of error that has been detected.

See also:

errno, fopen(), getc(), fgetc(), fgets(), ungetc()

Examples:

#include <stdio.h>

void main()
  {
    char buffer[80];

    while( gets( buffer ) != NULL )
      puts( buffer );
  }

Classification:

ANSI

Systems:

All (except DOS/PM)