[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


gets

Read a character array from stdin.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <stdio.h>
char *gets(char *s);
Parameters:

Parameters for this facility are:

s  
char s  
The string being written in to  

Remarks:

The gets() function reads characters from stdin and stores them sequentially in the character array pointed to by s. Characters are read until either a newline or an end-of-file is reached.

Unlike fgets(), the programmer cannot specify a limit on the number of characters to read. Also, gets() reads and ignores the newline character ('\n') so that it can advance the file position indicator to the next line. The newline character is not stored s. Like fgets(), gets() terminates the character string with a null character.

If an end-of-file is reached before any characters are read, gets() returns a null pointer (NULL) without affecting the character array at s. If a read error occurs, the contents of s may be corrupted.

Return:

gets() returns s if it is successful and returns a null pointer if it fails.

See Also:

"Wide Character and Byte Character Stream Orientation"

"fgets"

Example of gets() usage.:
#include <stdio.h>
#include <string.h>

int main(void)
{
	char buf[100];
	
	printf("Enter text lines to echo.\n");
	printf("Enter an empty line to quit.\n");

	// read character strings from the console
	// until an empty line is read
	while (strlen(gets(buf)) > 0)
		puts(buf);	// puts() appends a newline to its output
		
	printf("Done!\n");

	return 0;
}

Output:
Enter text lines to echo.
Enter an empty line to quit.
I'm experiencing deja-vu 
I'm experiencing deja-vu 
Now go to work
Now go to work

Done!


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000