例如:
char *s_gets(char *st, int n)
{
char *ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)//當ret_val等於空字符時,ret_val值為0,測試條件為假,循環結束
{
while (st[i]!='\n'&&st[i]!='\0')
{
i++;
}
if (st[i] == '\n')
st[i] == '\0';//將'\n'替換為'\0'
else
{
while (getchar()!='\n')//接受空字符,但是並不存儲
{
continue;
}
}
}
return ret_val;
}
函數目的是為了讀取最大字符數或第一個換行符為止,舍棄了之后多輸入的內容進入緩沖區,保證語句與鍵盤的同步
