问题描述
我对C还是比较陌生,想知道如何防止输入溢出...
I'm relatively new to C and would like to know how to prevent an overflow from input...
例如,我有:
scanf("%d", &a);
其中a是整数.
那么我该怎么做才能防止某人输入大于最大整数的数字?由于我正在处理的问题的限制,您必须使用 scanf
.如何限制输入?
So what I could I do to prevent someone from entering a number that's larger than max integer? Due to the constraints of the problem I'm working on, you HAVE to use scanf
. How do I go about restricting the input?
推荐答案
防止用户输入非常具有挑战性.
没有魔术手可以伸出来并阻止用户在键盘上跳动.
It is very challenging to prevent user input.
There is no magic hand to reach out and stop the user from beating away at the keyboard.
但是代码可以限制其读取的内容.
But code can limit what it reads.
-
scanf()
很难限制.它可能不会在溢出时设置errno
.代码可以将char
的数量限制为9.这是第一步,但是不能输入诸如"1000000000"之类的值.或"00000000000000001".
scanf()
is tough to limit. It may not seterrno
on overflow. Code can limit the number ofchar
to say 9. That's a first step but one can not enter values like "1000000000" or "00000000000000001".
// Assume INT_MAX = 2,147,483,647.
scanf("%9d", &a);
一种学究的方法将使用 fgetc()
.接下来是 unsigned
方法. int
需要花费更多时间.
A pedantic method would use fgetc()
. An unsigned
method follows. int
takes a bit more.
unsigned a = 0;
int ch = fgetc(stdin);
while (isspace(ch)) {
ch = fgetc(stdin);
}
while (isdigit(ch)) {
ch -= '0';
if (a >= UINTMAX/10 && (a > UINTMAX/10 || ch > UINTMAX%10)) {
a = UINTMAX;
break; // overflow detected.
}
a = a*10 + ch;
ch = fgetc(stdin);
}
ungetc(ch, stdin); // Put back `ch` as it was not used.
但是我更愿意改变目标,只是再次告诉用户,即使这意味着要读更多的字符.
But I prefer to change the goal and simply tell the user again, even if it means reading in more characters.
// 0:success or EOF
int Read_int(const char *prompt, int *dest, int min, int max) {
for (;;) {
char buf[(sizeof(int)*3 + 3)*2]; // 2x generous buffer
fputs(prompt, stdout);
if (fgets(buf, sizeof buf, stdin) == NULL) {
return EOF;
}
char *endptr;
errno = 0;
long l = strtol(buf, &endptr, 10);
// no conversion or junk at the end ....
if (buf == endptr || *endptr != '\n') {
continue;
}
if (!errno && l >= min && l <= max) {
*dest = (int) l;
return 0; // success
}
}
}
// Sample usage
int a;
Read_int("Enter an `int`\n", &a, INT_MIN, INT_MAX);
这篇关于如何防止使用scanf的输入溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!