函式名: cgets(在VC++6.0下為_cgets)
基本介紹
- 中文名:cgets
- 功 能: 從鍵盤得到一個字元串
- 用 法: char *_cgets( char *buffer );
- 所屬庫:conio.h
- 相關函式::getch、getche、gets、getchar
函式簡介,程式示例,
函式簡介
函式名: cgets(在VC++6.0下為_cgets)
功 能: 從鍵盤得到一個字元串
用 法: char *_cgets( char *buffer );
所屬庫:conio.h
程式示例
#include <conio.h>
#include <stdio.h>
int main(void)
{
char buffer[83];
char *p;
/* There's space for 80 characters plus the NULL terminator */
buffer[0] = 81;
printf("Input some chars:");
p = cgets(buffer);
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
/* Leave room for 5 characters plus the NULL terminator */
buffer[0] = 6;
printf("Input some chars:");
p = cgets(buffer);
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
return 0;
}
下面的例子來自MSDN
#include <conio.h>
#include <stdio.h>
#include <errno.h>
int main( void )
{
char buffer[83] = { 80 }; // Maximum characters in 1st byte
char *result;
printf( "Input line of text, followed by carriage return:\n");
// Input a line of text:
result = _cgets( buffer ); // C4996
// Note: _cgets is deprecated; consider using _cgets_s
if (!result)
{
printf( "An error occurred reading from the console:"
" error code %d\n", errno);
}
else
{
printf( "\nLine length = %d\nText = %s\n",
buffer[1], result );
}
}