C: 第二个 fgets 的错误字符
C: Wrong Characters with Second fgets
我有这个代码:
char temp;
scanf("%c",&temp);
fgets(variable1,50,stdin);
strtok(variable1, "\n");
printf("%s ", variable1);
这得到一个可能有空格的字符串,赋值给变量variable1
。稍后,我可以毫无问题地打印字符串。
当我向代码中添加其他 fgets
for 获取其他变量中的其他字符串时出现问题。
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
始终正常工作。但是我尝试用 %s
variable2
打印一些短语,但结果没有得到第一个字符,只有第二个 scanf
。如果我输入 HELLO,variable2
就是 ELLO。
我已经使用另一个 temp
变量、另一个数据等进行了测试。但总是得到同样的错误。
为什么会这样?
更新
想要查询更多的信息。我使用 scanf
是因为,如果我不使用它,程序在等待字符串时不会暂停。我使用 strtok(variable1, "\n");
删除换行符。
此程序在 while
和 switch 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 不应该存在?
这段代码对我来说很好用:
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
根据 OP 对 post 的最新编辑和评论重新编写...
您描述了需要简单地从用户输入中获取两个字符串,从每个字符串中删除换行符,然后将它们打包成一条消息发送给 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
我有这个代码:
char temp;
scanf("%c",&temp);
fgets(variable1,50,stdin);
strtok(variable1, "\n");
printf("%s ", variable1);
这得到一个可能有空格的字符串,赋值给变量variable1
。稍后,我可以毫无问题地打印字符串。
当我向代码中添加其他 fgets
for 获取其他变量中的其他字符串时出现问题。
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
始终正常工作。但是我尝试用 %s
variable2
打印一些短语,但结果没有得到第一个字符,只有第二个 scanf
。如果我输入 HELLO,variable2
就是 ELLO。
我已经使用另一个 temp
变量、另一个数据等进行了测试。但总是得到同样的错误。
为什么会这样?
更新
想要查询更多的信息。我使用 scanf
是因为,如果我不使用它,程序在等待字符串时不会暂停。我使用 strtok(variable1, "\n");
删除换行符。
此程序在 while
和 switch 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 不应该存在?
这段代码对我来说很好用:
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
根据 OP 对 post 的最新编辑和评论重新编写...
您描述了需要简单地从用户输入中获取两个字符串,从每个字符串中删除换行符,然后将它们打包成一条消息发送给 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