【问题标题】:Fgets and sscanf not waiting for input CFgets 和 sscanf 不等待输入 C
【发布时间】:2025-12-26 20:05:12
【问题描述】:

最初我使用的是 scanf,但我遇到了换行符卡在标准输入中的问题。我读过的所有内容都在说要切换到 fgets 并改用 sscanf 。有了这个,我决定改用那个……但这仍然行不通。您将在下面找到我的代码。我的问题是,我的 fgets 和 sscanf 做错了什么导致它不等待用户输入?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct f_in{
    char outline;
    int lines;
    int rows;
    int num_args;
} F_IN;

typedef struct args_in {
    char in_string[20];
    int t_format;
} ARGS_IN;

void printInterface(char argQs[5][50], char argChar);

int main(int argv, char** argc){
    char defaultQuestions[5][50] = { { "1) What char for border?" }
            , { "2) Add question" }
            , { "3) Remove Question" }
            , { "4) Print last answers" }
    , { "5) Exit" } };
    int commandEntry, exitFlag;
    char borderChar = '*', addQ[50],userInp[1];

    exitFlag = 1;

    while (exitFlag){
            printInterface(defaultQuestions, borderChar);

            printf("Enter the integer value for the command you wish to select:  ");
            fgets(userInp, sizeof(userInp),stdin);
            sscanf(userInp,"%d", &commandEntry);
            printf("\nYou selected: %s\n", defaultQuestions[commandEntry - 1]);

            userInp[0] = 0;

            if (commandEntry == 1){
                    printf("Please enter the character you wish to be the border:  ");
                    fgets(userInp,sizeof(userInp),stdin);
                    sscanf(userInp,"%c",&borderChar);
            }
            else if (commandEntry == 2){
                    printf("What question would you like to add? (only enter 50 char max)\n");
                    fgets(addQ, 50, stdin);
                    printf("This was your question: %s", addQ);
            }
            else if (commandEntry == 5){
                    printf("Goodbye!\n");
                    exitFlag = 0;
            }
    }
    return 0;
}

void printInterface(char argQs[5][50], char argChar){
    int i, j;
    int lineCnt = 13;
    int borderLen = 75;

    for (i = 0; i<100; i++){
            printf("\n");
    }

    for (i = 0; i<lineCnt; i++){

            if (i == 0 || i == lineCnt - 1){
            for (j = 0; j<borderLen; j++){
                            printf("%c", argChar);
                    }
                    printf("\n");
            }

            else if (i >= 3 && i <= 10){
                    printf("%c    %s", argChar, argQs[i - 3]);
                    for (j = 0; j < ((borderLen - strlen(argQs[i - 3]))-6); j++){
                            printf(" ");
                    }
                    printf("%c\n", argChar);
            }

            else{
                    for (j = 0; j<borderLen; j++){
                            if (j == 0){
                                    printf("%c", argChar);
                            }
                            else if (j == (borderLen - 1)){
                                    printf("%c\n", argChar);
                            }
                            else{

                    for (j = 0; j<borderLen; j++){
                            if (j == 0){
                                    printf("%c", argChar);
                            }
                            else if (j == (borderLen - 1)){
                                    printf("%c\n", argChar);
                            }
                            else{                
                                    printf(" ");
                            }
                    }
            }        
    }                  

    for (i = 0; i<10; i++){ 
            printf("\n");
    }

}

【问题讨论】:

  • 究竟是什么不工作?输入是什么?什么是预期的输出 ?修改您的问题并明确说明。
  • @chux,我走那条路的原因是因为当时我已经想到,因为我只需要接受数字 1-5 作为我的输入,一个带有一个字符的 char 数组将是充足的。我完全忘记了空终止符,因此问题。
  • 发布的代码包含几个“神奇”数字。 '幻数是没有根据的数字。 IE。 5, 50 建议使用enum 语句或#define 语句为这些“神奇”数字赋予有意义的名称,然后在整个代码中使用这些有意义的名称。
  • 这一行:fgets(userInp, sizeof(userInp),stdin); 实际上是说输入 0 个字符,因为fgets() 总是在输入字段数据的末尾附加一个 NUL 字符,并且由于输入字段只有 1 个字符长这将导致输入字段 userInp 是 NUL 字符。
  • if (commandEntry == 1){ 之类的语句开头的代码块会更好地合并到switch() 语句的case 语句中。并且它们应该是最终测试(默认语句),向用户打印有关无效输入的消息

标签: c fgets scanf


【解决方案1】:

"userInp[1] 只允许足够的内存来存储终止的'\0'" - 用户312023

【讨论】: