【问题标题】:Using fgetc with stdin pointer. Error handling input将 fgetc 与标准输入指针一起使用。错误处理输入
【发布时间】:2023-04-01 06:02:01
【问题描述】:

我只想问为什么我的函数read_int 不接受我来自fgetc 的输入作为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 中的 c 用于 c 字符。它从文件中读取字符。

标签: c


【解决方案1】:

fgetc() 用于从流中读取字节,一次返回一个字符。

要读取int,使用scanf("%d", &f),读取doublescanf("%lf", &d)fd分别定义为int f;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_intread_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;
}

【讨论】:

  • 好的,首先,我需要在这个练习中使用 fgetc(stdin),所以我不能在这个练习上真正使用 scanf。其次,目标不是打印整数,而是在我输入时产生错误消息像这样的东西,23.44 23abc 23. 23+44
  • @Elo:好的。感谢您阐明您的目标。您应该从stdin 读取字节并将它们转换为整数,只要您获得数字即可。对于read_int() 来说相对容易,但在一般情况下要为read_double() 做好完美的工作却非常困难。
  • @chqrlie 你能举个例子吗?我是一个完整的 C 初学者。我的意思是将标准输入上的数字转换为整数。
  • @Elo:我用简单而有效的转换代码更新了答案。
猜你喜欢
  • 2011-06-22
  • 2012-04-16
  • 2021-03-17
  • 2011-02-19
  • 2013-06-13
  • 1970-01-01
  • 2022-07-10
  • 2012-10-19
  • 2011-05-01
相关资源
最近更新 更多