【问题标题】:C: Wrong Characters with Second fgetsC:第二个 fget 的错误字符
【发布时间】:2020-09-08 12:20:24
【问题描述】:

我有这个代码:

char temp;
scanf("%c",&temp);
fgets(variable1,50,stdin);
strtok(variable1, "\n");

printf("%s ", variable1);

这会得到一个可能包含空格的字符串并将其分配给变量variable1。之后,我可以毫无问题地打印字符串。

当我在代码中添加其他 fgetsfor 获取其他变量中的其他字符串时,问题就出现了。

if (1) {
    scanf("%c",&temp);
    fgets(variable2,50,stdin);
    strtok(variable2, "\n");

    printf("%s ", variable2);
}

完成的结果是:

char temp;
scanf("%c",&temp);
fgets(variable1,50,stdin);
strtok(variable1, "\n");

printf("%s ", variable1);

if (1) {
    scanf("%c",&temp);
    fgets(variable2,50,stdin);
    strtok(variable2, "\n");

    printf("%s ", variable2);
}

variable1 始终正常工作。但我尝试用%svariable2 打印一些短语,但结果没有得到第一个字符,只有第二个scanf。如果我打招呼,variable2 就是 ELLO。

我已经使用另一个temp 变量、另一个数据等进行了测试。但总是得到同样的错误。

为什么会这样?

更新 了解更多信息。我使用scanf,因为如果我不使用它,程序在等待字符串时不会暂停。我使用strtok(variable1, "\n"); 删除换行符。

该程序位于whileswitch case 中。我放了完整的代码:

case 4: printf( "Put equipo: "); 
    scanf("%c",&temp);
    fgets(equipo,50,stdin);
    strtok(equipo, "\n");

    if (Exists(equipo)) {
        printf("Put Piloto ");
        scanf("%c",&temp);
        fgets(piloto,50,stdin);
        strtok(piloto, "\n");
        printf("You said %s and %s", equipo, piloto);
    }

    break;

如果我像 Equipo HELLO 和 Piloto FRIEND 这样引入,输出是:

You said HELLO and RIEND

【问题讨论】:

  • scanf("%c", &temp); 的原因是什么?如果你想跳过换行符,fgets() 已经读到了。
  • 请发minimal reproducible example,包括所有变量声明。
  • 您在扫描variable2 后打印variable1,因此您的variable1 可能不像您想象的那样“正确”。
  • if()printf("%s ", variable1);。那是真正的代码吗?真的是printf("%s ", variable2);吗?
  • 如果他们输入的字符超过 50 个,您希望发生什么?

标签: c char printf fgets


【解决方案1】:

根据 OP 的最新编辑重新编写发布,以及 cmets...

您描述了需要从用户输入中简单地获取两个字符串,从每个字符串中删除换行符,然后将两者打包成一条消息发送给stdout。如果此描述确实符合您的需要,请更改此代码部分:

...
scanf("%c",&temp);
fgets(equipo,50,stdin);
strtok(equipo, "\n");

if (Exists(equipo)) {
    printf("Put Piloto ");
    scanf("%c",&temp);
    fgets(piloto,50,stdin);
    strtok(piloto, "\n");
    printf("You said %s and %s", equipo, piloto);
...

对此:

...
printf("enter equipo: ");
if(fgets(equipo, sizeof(equipo), stdin))
{
    equipo[strcspn(equipo, "\n")] = 0; //remove newline 
    printf("enter piloto: ");
    if(fgets(piloto, sizeof(piloto), stdin))
    {
        piloto[strcspn(piloto, "\n")] = 0; 
        printf("You said %s and %s", equipo, piloto);
    }
}
...

注意:strtok(piloto, "\n"); 有效,但如果用户只是点击<return> 会出现问题

顺便说一句,here are some other interesting ways to clear the newline

【讨论】:

  • @M.M - 我想我明白你的意思了。错误的功能选择和 OP 更新回答后,甚至不会解决主要请求。完全重写。谢谢
【解决方案2】:

我不明白您的示例代码。你能用你的实际代码更新问题吗?

我认为 scanf 不应该在那里?

这段代码对我来说很好用:

char var1[50], var2[50];
fgets(var1, 50, stdin);
strtok(var1, "\n");
printf("Var1: %s\n", var1);
if (1) {
        fgets(var2, 50, stdin);
        strtok(var2, "\n");
        printf("Var2 %s\n", var2);
}

输出:

Test1
Var1: Test1
Test2
Var2 Test2

【讨论】:

  • 如果我这样做,程序不会暂停等待用户输入的字符串,直接选择一个`\n`作为var1...
  • 你应该总是得到 fgets 的返回值
猜你喜欢
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
相关资源
最近更新 更多