其他分享
首页 > 其他分享> > 利用fgets()创建一个较为完善的输入函数

利用fgets()创建一个较为完善的输入函数

作者:互联网

//创建一个较为完善的输入函数
#include	<stdio.h>
#define SIZE 80
void input_char(char *st,int n);
 
int	main(void)
{
	char names[SIZE];
	
	input_char(names,SIZE);
	
	puts("print the characters :");
	puts(names);
	
	return	0;	
}

void input_char(char *st,int n)
{
	char *ret_val;
	puts("Please enter a characters you want:");
	while (*(ret_val=fgets(st,n,stdin))=='\n')
	{
		puts("The Enter Key can't be the first char!");
		puts("Input again!");
		continue;
	}
	if (ret_val != NULL)
	{
		int i=0;
		while (st[i] != '\n'&&st[i] != '\0')
		{
			i++;
		}
		if (st[i] == '\n')
		{
			st[i]='\0';
		}
		else//不等于换行符说明输入缓存流中有残余字符 
		{
			while (getchar() != '\n')
			{
				continue;
			}
		}
	}
	return;
} 

标签:函数,puts,int,ret,st,char,names,fgets,输入
来源: https://www.cnblogs.com/OldSword-JY/p/15708071.html