c - 将 fgetc 与标准输入指针一起使用。错误处理输入
问题描述
我只想问为什么我的函数read_int
不接受我的输入fgetc
as int
?(我认为是这种情况,因为我总是输入一个整数,但它仍然会转到int
显示错误的函数。
这是我的代码(来自我的教授的 skel 代码):
int main() {
int the_int;
double the_double;
printf("Doing tests of read_int and read_double functions ...\n\n");
printf("Please try to enter an int: ");
the_int = read_int();
printf("read_int succeeded. It read a value of %d.\n", the_int);
printf("Please try to enter a double: ");
the_double = read_double();
printf("read_double succeeded. It read a value of %g.\n", the_double);
return 0;
}
int read_int(void) {
int f = fgetc(stdin);
if (fgetc(stdin) != EOF) {
printf("Sorry that is invalid.");
exit(1);
}
return f;
}
解决方案
fgetc()
用于从流中读取字节,一次返回一个字符。
阅读 a int
,使用scanf("%d", &f)
和 阅读 a double
,scanf("%lf", &d)
withf
和分别d
定义为int f;
and double d;
。
还要<stdio.h>
在调用它们之前包含并定义函数。
这是修改后的版本:
#include <stdio.h>
int read_int(void) {
int f;
if (scanf("%d", &f) != 1) {
printf("Sorry that input is invalid.\n");
exit(1);
}
return f;
}
double read_double(void) {
double d;
if (scanf("%lf", &d) != 1) {
printf("Sorry that input is invalid.\n");
exit(1);
}
return d;
}
int main(void) {
int the_int;
double the_double;
printf("Doing tests of read_int and read_double functions ...\n\n");
printf("Please try to enter an int: ");
the_int = read_int();
printf("read_int succeeded. It read a value of %d.\n", the_int);
printf("Please try to enter a double: ");
the_double = read_double();
printf("read_double succeeded. It read a value of %g.\n", the_double);
return 0;
}
如果您需要使用fgetc()
读取输入流,这里有修改版本read_int
并将read_double
这些字节转换为实际数字:
#include <stdlib.h>
int read_int(void) {
int c;
int sign = 1;
int f = 0;
/* skip leading white space */
while ((c = fgetc(stdin)) == ' ' || c == '\n')
continue;
/* handle optional sign */
if (c == '-') {
sign = -1;
c = fgetc(stdin);
} else
if (c == '+') {
c = fgetc(stdin);
}
/* convert digits */
if (c >= '0' && c <= '9') {
f = c - '0';
while ((c = fgetc(stdin)) >= '0' && c <= '9')
f = f * 10 + c - '0';
if (c != EOF)
ungetc(c, stdin);
} else {
/* no digits: fatal error */
if (c == EOF)
printf("Unexpected end of file\n");
else
printf("Sorry that input is invalid: %c\n", c);
exit(1);
}
/* apply sign and return value */
return sign * f;
}
double read_double(void) {
char buf[80];
int c;
size_t pos = 0;
double d;
char *p;
/* read a line of input */
while (pos < sizeof(buf) - 1 && (c = fgetc(stdin)) != EOF && c != '\n')
buf[pos++] = c;
buf[pos] = '\0';
/* use standard library function to convert the number */
p = buf;
d = strtod(buf, &p);
/* if conversion failed, report fatal error */
if (p == buf) {
printf("Sorry this input is invalid: %s\n", buf);
exit(1);
}
return d;
}
推荐阅读
- c# - 无法加载 DLL 'Microsoft.DiaSymReader.Native.amd64.dll
- python - 如何使用for循环和条件不规则地交错数据帧?
- twilio - 符合 Twilio PCI 标准
在带有 Studio 的函数小部件中 - android - WebView 中的 Google 安全搜索
- javascript - 如何使用 javascript/jquery 过滤数组中的数据?
- java - JAVA LDAP 错误 javax.naming.NamingException:[LDAP:错误代码 1 - 000004DC:LdapErr:DSID-0C09075A
- angular - 我的 GOIBIBO 航班搜索 API 无法正常工作
- php - 绕过限制从 Telegram Bot 发送 TXT 文件
- python - 如何以 16:9 的纵横比计算最接近的分辨率。蟒蛇 3
- javascript - React - 无法以编程方式从输入文本元素调度 onChange 事件