[英]Is there a way to flush the first input buffer in successive fgets() functions?
我使用fgets()
function 并使用getchar()
清除剩余'\n'
输入缓冲区。
这时候就出现了问题, getchar()
往往没用,因为输入缓冲区中剩余的'\n'
转义序列只有在超过从用户接收到的字符串长度时才会记录下来。
如上所述使用scanf()
(例如: scanf(" %s\n", &test);
),有一种在%s
之前给出空格的方法。 由于scanf()
无法限制输入字符串的长度,因此我无法使用它,因为存在溢出的风险。
怎么做?
char student_number[10]
char name[20]
char phone_number[14]
printf("student Number: ");
fgets(student_number, 10, stdin);
getchar();
printf("name: ");
fgets(name, 20, stdin);
getchar();
printf("phone Number: ");
fgets(phone_number, 14, stdin);
getchar();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.