gets

Gets a string of characters from stdin and stores them in an array.

Library:LibC
Classification:ANSI
Service:File and Directory I/O

Syntax

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

Parameters

buf

(OUT) Points to the array into which the characters are to be stored.

Return Values

If successful, returns buf. Otherwise, returns a NULL pointer. If the stream is at end-of-file, gets also sets the end-of-file indicator for the stream. If a read error occurs, gets also sets errno to one of the following:

Decimal

Constant

Description

4

EBADF

The file descriptor is not valid for reading.

5

ENOMEM

Insufficient storage space is available.

24

EAGAIN

The O_NONBLOCK flag is set for the file descriptor, and the process cannot immediately perform the read operation.

25

ENXIO

A request was made of a nonexistent device, or the request was outside the capabilities of the device.

28

EIO

The process is orphaned and cannot write to the file.

63

EINTR

A signal interrupted the read operation.

81

EOVERFLOW

The file is a regular file and an attempt was made to read at or beyond the offset maximum associated with the corresponding stream.

Remarks

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 a newline character is read or an end-of-file condition is encountered. Any newline character is discarded, and a null-terminating character is placed immediately after the last character read into the array.

See Also