从文件中扫描字符的问题

Issue with scanning a character from a file

我的代码在从文件中扫描字符时出现了问题。这是我的代码:

int main(int argc, char **argv){
    FILE *ifp = fopen("input.txt", "r");

    char check, key;
    char buffer[MAX_STRING_LENGTH + 1];
    char str[MAX_STRING_LENGTH + 1];
    node *head = NULL;

    fscanf(ifp, "%s", buffer);
    head = stringToList(buffer);

    while(fscanf(ifp, "%c", &check) != EOF){
        switch(check){
        case '@':
            //fflush(stdin);            //this is commented out because fflush
            fscanf(ifp, "%c", &key);         //did not seem to solve the issue
            fscanf(ifp, "%s", str);
            printf("%c, %s\n", key, str);
            head = replaceChar(head, key, str);
            break;

        //more cases follow, but they are irrelevant to this question

我的问题是我的程序在调用时无法将字符扫描到键中。例如,当文件中特定案例的输入显示

@ r ri

打印语句给出以下内容:

( ), (r)

在应该打印 r 的打印语句中有一个空白 space,据我所知,字符是扫描到字符串中。因此该程序功能失调。我需要知道如何正确扫描这个字符。谢谢。

fscanf(ifp, " %c", &key); then fscanf(ifp, " %s", str); notice the additional spaces. You are reading a ' ' after reading the '@'. – David C. Rankin

这已经回答了我的问题。很奇怪 C 会被这么简单的东西弄乱。