Routine Required Header Compatibility
_cgets <conio.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
_cgets returns a pointer to the start of the string, at buffer[2]. There is no error return.
Parameter
buffer
Storage location for data
Remarks
The _cgets function reads a string of characters from the console and stores the string and its length in the location pointed to by buffer. The buffer parameter must be a pointer to a character array. The first element of the array, buffer[0], must contain the maximum length (in characters) of the string to be read. The array must contain enough elements to hold the string, a terminating null character ('\0'), and two additional bytes. The function reads characters until a carriage-return – linefeed (CR-LF) combination or the specified number of characters is read. The string is stored starting at buffer[2]. If the function reads a CR-LF, it stores the null character ('\0'). _cgets then stores the actual length of the string in the second array element, buffer [1]. Because all editing keys are active when _cgets is called, pressing F3 repeats the last entry.
Example
/* CGETS.C: This program creates a buffer and initializes
* the first byte to the size of the buffer: 2. Next, the
* program accepts an input string using _cgets and displays
* the size and text of that string.
*/
#include <conio.h>
#include <stdio.h>
void main( void )
{
char buffer[82] = { 80 }; /* Maximum characters in 1st byte */
char *result;
printf( "Input line of text, followed by carriage return:\n");
result = _cgets( buffer ); /* Input a line of text */
printf( "\nLine length = %d\nText = %s\n", buffer[1], result );
}
Output
Input line of text, followed by carriage return:
This is a line of text
Console and Port I/O
These routines read and write on your console or on the specified port. The console I/O routines are not compatible with stream I/O or low-level I/O library routines. The console or port does not have to be opened or closed before I/O is performed, so there are no open or close routines in this category. In Windows NT and Windows 95, the output from these functions is always directed to the console and cannot be redirected.
Console and Port I/O Routines
Routine Use
_cgets Read string from console
_cprintf Write formatted data to console
_cputs Write string to console
_cscanf Read formatted data from console
_getch Read character from console
_getche Read character from console and echo it
_inp Read one byte from specified I/O port
_inpd Read double word from specified I/O port
_inpw Read 2-byte word from specified I/O port
_kbhit Check for keystroke at console; use before attempting to read from console
_outp Write one byte to specified I/O port
_outpd Write double word to specified I/O port
_outpw Write word to specified I/O port
_putch Write character to console
_ungetch “Unget” last character read from console so it becomes next character read