gtpc2m40 | C/C++ Language Support User's Guide |
This function reads a string from the standard input stream.
Format
#include <stdio.h> char *gets(char *buffer);
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 |
---|
|
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