gtpc2m40C/C++ Language Support User's Guide

gets-Obtain Input String

This function reads a string from the standard input stream.

Format

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

buffer
This argument is a pointer to available working storage of sufficient size to hold the expected input.

This function reads bytes from the standard input stream stdin, and stores them in the array pointed to by buffer. The line consists of all characters up to and including the first new-line character (\n) or end-of-file (EOF). The gets function discards any new-line character, and the null character (\0) is placed immediately after the last byte read. If there is an error, the value stored in buffer is undefined.

The gets function has the same restriction as any read operation, such as a read immediately following a write, or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must be an intervening reposition unless an EOF has been reached.

TARGET(TPF) restrictions

The gets function in the TARGET(TPF) library is a limited and non-standard substitute for the standard gets function. The TARGET(TPF) library has no support for files, stdin, or file redirection.

The TARGET(TPF) version of the gets function does not have the same effect as the ISO-C version. Use of both versions in the same application can have unpredictable results.

Normal Return

If successful, the gets function returns its argument.

Error Return

A NULL pointer returned value indicates an error or an EOF condition with no characters read.

Use ferror or feof functions to determine which of these conditions occurred. Note that EOF is only reached when an attempt is made to read past the last byte of data. Reading up to and including the last byte of data does not turn on the EOF indicator.

Programming Considerations

TARGET(TPF) restrictions

  • Owing to the large diversity of terminal devices and network support in the TPF system, your installation must code this function to work with its requirements. All programming considerations related to the use of this function, therefore, depend on the manner of implementation.
  • A valid input message block must exist in a location established by your installation. If the block cannot be found or bears an improper record ID, zero is returned.
  • This function returns the address of a line that originally ended with a new-line character (\n). This character is replaced with a hex zero (\0) so that the returned line may be treated as a character string.
  • Subsequent calls to gets function will return the beginning address of the next new-line-terminated string, if any, in the message. When no more input exists, a null pointer is returned.

Examples

The following example gets a line of input from stdin.

#include <stdio.h>
#define MAX_LINE 100
 
int main(void)
{
   char line[MAX_LINE];
   char *result;
 
   printf("Enter string:\n");
   if ((result = gets(line)) != NULL)
      printf("string is %s\n",result);
   else
      if (ferror(stdin))
        printf("Error\n");
}

Related Information