我在努力学习c,Zed A.Shaw的《ex25的艰难之路》
ex25

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "dbg.h"

#define MAX_DATA 100

int read_string(char **out_string, int max_buffer)
{
    *out_string = calloc(1, max_buffer + 1);
    check_mem(*out_string);

    char *result = fgets(*out_string, max_buffer, stdin);
    check(result != NULL, "Input error.");

    return 0;

error:
    if (*out_string) free(*out_string);
    *out_string = NULL;
    return -1;
}

int read_int(int *out_int)
{
    char *input = NULL;
    int rc = read_string(&input, MAX_DATA);
    check(rc == 0, "Failed to read number.");

    *out_int = atoi(input);

    free(input);
    return 0;

error:
    if (input) free(input);
    return -1;
}

int read_scan(const char *fmt, ...)
{
    int i = 0;
    int rc = 0;
    int *out_int = NULL;
    char *out_char = NULL;
    char **out_string = NULL;
    int max_buffer = 0;

    va_list argp;
    va_start(argp, fmt);

    for (i = 0; fmt[i] != '\0'; i++) {
        if (fmt[i] == '%') {
            i++;
            switch (fmt[i]) {
                case '\0':
                    sentinel("Invalid format, you ended with %%.");
                    break;

                case 'd':
                    out_int = va_arg(argp, int *);
                    rc = read_int(out_int);
                    check(rc == 0, "Failed to read int.");
                    break;

                case 'c':
                    out_char = va_arg(argp, char *);
                    *out_char = fgetc(stdin);
                    break;

                case 's':
                    max_buffer = va_arg(argp, int);
                    out_string = va_arg(argp, char **);
                    rc = read_string(out_string, max_buffer);
                    check(rc == 0, "Failed to read string.");
                    break;

                default:
                    sentinel("Invalid format.");
            }
        } else {
            fgetc(stdin);
        }

        check(!feof(stdin) && !ferror(stdin), "Input error.");
    }

    va_end(argp);
    return 0;

error:
    va_end(argp);
    return -1;
}

int main(int argc, char *argv[])
{
    char *first_name = NULL;
    char initial = ' ';
    char *last_name = NULL;
    int age = 0;

    printf("What's your first name? ");
    int rc = read_scan("%s", MAX_DATA, &first_name);
    check(rc == 0, "Failed first name.");

    printf("What's your initial? ");
    rc = read_scan("%c\n", &initial);
    check(rc == 0, "Failed initial.");

    printf("What's your last name? ");
    rc = read_scan("%s", MAX_DATA, &last_name);
    check(rc == 0, "Failed last name.");

    printf("How old are you? ");
    rc = read_scan("%d", &age);

    printf("---- RESULTS ----\n");
    printf("First Name: %s", first_name);
    printf("Initial: '%c'\n", initial);
    printf("Last Name: %s", last_name);
    printf("Age: %d\n", age);

    free(first_name);
    free(last_name);
    return 0;
error:
    return -1;
}

我对line 109中read_scan的第一个参数感到困惑
原始代码运行良好输出:
What's your first name? zed
What's your initial? A
What's your last name? shaw
How old are you? 18
---- RESULTS ----
First Name: zed
Initial: 'A'
Last Name: shaw
Age: 18

但是,如果我删除line 109rc = read_scan("%c", &initial);中的'\n',它将跳过下一个问题,我无法找出答案。
我认为“\n”的影响是for loop不会进入line 83fgetc(stdin);
输出为:
What's your first name? zed
What's your initial? A
What's your last name? How old are you? shaw
---- RESULTS ----
First Name: zed
Initial: 'A'
Last Name:
Age: 0

谢谢你的帮助!

最佳答案

"%c\n"read_scan()中的字符表示读取和捕获一个字符,并读取和丢弃另一个字符您可以使用X@而不是\n,它的工作原理相同从格式中删除换行符时,字符后面的换行符将保留在输入中然后,下一个用read_scan()调用"%s",它会一直读取到下一个换行符,但是下一个换行符已经在输入流中,所以它会立即返回。
注意,如果您键入的是一个单词而不是首字母,或者您没有中间的首字母(me!),事情会以不同的方式出错。

关于c - 学习EX25的复杂方法使Fgetc Stdin感到困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49869532/

10-10 22:15