【问题标题】:Scanf() is being skipped when trying to read including whitespaces [duplicate]尝试读取包含空格时跳过 Scanf() [重复]
【发布时间】:2012-12-20 21:51:18
【问题描述】:

可能重复:
scanf: “%[^\n]” skips the 2nd input but “ %[^\n]” does not. why?

基本上我有一个客户结构,我要在其中输入客户详细信息,其中之一是地址。当然地址是一个句子,因为这是一个没有图形的基本文本程序。

我正在尝试使用scanf("%[^\n]",&VARIABLE) 方法,因为该方法在以前的程序中有效。 但是在这里,输入被跳过。我尝试在接受此输入之前刷新缓冲区,但没有任何区别。我还尝试创建另一个字符串并将我的输入传递给它,然后将数据复制到我的结构中,但这也不起作用。

这是我的代码 - 问题出现在第 4 次 scanf("%[^\n]",&myCust.address) :(注意:这是一项正在进行的工作,因此您现在可能会看到一些额外的打印和内容)

void addNewCustomer()
{
    struct customer myCust;
    printf("\n\nNEW CUSTOMER ADDITION\n");
    printf("\nEnter customer id : ");
    scanf("%s",&myCust.idNumber);
    printf("\nEnter customer name : ");
    scanf("%s",&myCust.name);
    printf("\nEnter customer surname : ");
    scanf("%s",&myCust.surname);
    fflush(stdin);
    printf("\nEnter customer address : ");
    scanf("%[^\n]",&myCust.address);
    printf("\nEnter customer telephone : ");
    scanf("%s",&myCust.telephone);
    printf("\nEnter customer mobile : ");
    scanf("%s",&myCust.mobile);
    printf("\nEnter customer e-mail : ");
    scanf("%s",&myCust.email);

    FILE *fp;

    fp = fopen("/Users/alexeidebono/Dropbox/Customer_Application/customers.dat","a");
    if (fp == NULL) {
        printf("The File Could Not Be Opened.\n");
        exit(0);
    }
    else{
        printf("File Successfully Open\n");
   fprintf(fp,"%s*%s*%s*%s*%s*%s*%s#\n",myCust.idNumber,myCust.name,myCust.surname,myCust.address,myCust.telephone,myCust.mobile,myCust.email);
    fclose(fp);
    printf("Writing successfully completed and the file is closed!!\n");
   }
}

如果你想要我的结构代码在这里(虽然我不认为结构本身是这个问题的原因)

struct customer
{
    char idNumber[11]; 
    char name[11];
    char surname[15];
    char address[30];
    char telephone[14];
    char mobile[14];
    char email[21];


};

【问题讨论】:

  • 这可能每周被问 20 次。
  • 如果我是你,我不会在 scanf 中使用& 来读取 C 字符串。

标签: c scanf


【解决方案1】:
scanf("%s",&myCust.surname);

scanf 在输入缓冲区中留下一个换行符

fflush(stdin);

这是标准未定义的行为,根据我的经验,即使库承诺它会可靠地工作,它也不能可靠地工作。

printf("\nEnter customer address : ");
scanf("%[^\n]",&myCust.address);

这会立即找到换行符。所以它不会读入任何内容,因为它首先遇到换行符。通过在格式中包含空格使其首先跳过空格,

scanf(" %[^\n]",&myCust.address);

或使用fgetsgetline(如果您使用的是 POSIX 系统)读取整行。

【讨论】:

  • fgets 没有工作,因为它仍然被跳过(我忘了在我的问题中提到这一点,因为我也试过了 - 对此感到抱歉)但是,在地址 scanf 中留一个空格就可以了。感谢您的回答-现在一切都说得通了。干杯队友
  • 如果您使用fgets,则应将其用于所有条目。由于这会消耗换行符,因此下一个输入不会立即找到一个。混合使用 scanffgets 并不是一个好主意,因为换行符的不同处理会导致严重的头痛。
  • 我会接受你的意见,因为 fgets 似乎是最好的选择。
  • 只是不要忘记fgets 将换行符存储在缓冲区中(除非输入的行太长,在这种情况下,您必须在下一次输入之前清除输入缓冲区)。
【解决方案2】:

标题:

Scanf() 在尝试读取包含空格时被跳过

是的。那不是错误。这就是scanf() 的工作原理(请阅读您下次尝试使用的函数的文档)。如果你想得到一整行,不管它的内容,使用fgets()

char buf[1024];
fgets(buf, sizeof(buf), stdin);

【讨论】:

  • 刚刚尝试过这个方法 - 它仍然被跳过不幸的是我尝试了两种方法 1. 在上述情况下,我传递了我的结构变量而不是“buf” 2. 我使用了另一个字符(将其命名为 test for为了这个缘故)并尝试只向其中输入数据(而不将其进一步传递给我的结构),但它仍然跳过它
  • @alexeidebono 来点代码清理一下怎么样?你确定你在正确的地方做这件事吗? fgets() 真的应该得到一整行,不管它是否包含空格。
  • 这虽然有效 - scanf(" %[^\n]",&myCust.address); (开头留一个空格)
【解决方案3】:

如果跳过了 scanf() 命令,这通常是因为您输入了以前扫描的垃圾。

尝试在跳过的之前添加另一个 scanf(&junk)。 junk 应该是 char。

【讨论】:

    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 2019-02-21
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多