在 Windows 上对字符串使用 scanf 时双击 Ctrl+D 输入
Double Ctrl+D input when using scanf for strings on Windows
情况是这样的,我有一段代码读取值,直到用户输入 Ctrl+D,但问题是我必须输入两次才能成功退出循环,我认为这与事实上,Ctrl+D 与缓冲区的剩余部分一起被插入到字符串中,因此没有注册为 EOF,只是字符串中的另一个字符,只有第二个 Ctrl+D 将被保存在一个 int 变量中,实际上停止了阅读循环。为了解决这个问题,我添加了 while(getchar() != '\n') 以查看它是否解决了问题,但没有结束。
这是我的代码:
typedef struct {
char nome[50];
int tempo;
int ncertas;
} equipa;
int ler_equipas(equipa *resultados)
{
int x, r1, r2 ,r3 ,r4, r11, r22, r33 ,r44, rcertas = 0, i = 0;
scanf ("%d %d %d %d", &r1, &r2, &r3, &r4);
while (scanf ("%s %d %d %d %d %d", resultados[i].nome, &resultados[i].tempo, &r11, &r22, &r33, &r44 ) == 6 )
{
i++;
if (r1 == r11) rcertas++;
if (r2 == r22) rcertas++;
if (r3 == r33) rcertas++;
if (r4 == r44) rcertas++;
resultados[i-1].ncertas = rcertas;
rcertas = 0;
while((x=getchar()) != '\n');
}
return i;
}
input/output结果如下(">>"表示输出):
1 2 3 4
>> correct answers are 1 2 3 4
Team1 234 1 2 3 4
>> values inputed for team time and answers: Team1 234 1 2 3 4
>> getchar value was (ASCII): 10
>> Next read:
Team2 400 1 3 2 4
>> values inputed for team time and answers: Team2 400 1 3 2 4
>> getchar value was (ASCII): 10
>> Next read:
>>
^D
^D
>> return i was reached
>> Number of teams: 2
>> Team "Team1" took 234 seconds and answered 4 questions correctly
>> Team "Team2" took 400 seconds and answered 2 questions correctly
我还没有试过这个。但我记得有同样的问题...
试试这个:How to clear input buffer in C?
while ((c = getchar()) != '\n' && c != EOF) { }
这是一种清除输入缓冲区的方法。
但这是在调用 scanf();
清除输入缓冲区之后。
此致
由于您正在使用 windows,因此您应该使用 ctrl+z
来模拟 EOF
见
当您使用 ctrl+d
时,第一个 ctrl+d
与初始 %s
匹配,因此扫描继续。
第二个 ctrl+d
将不匹配 %d
因此扫描终止。由于扫描的元素数不是 6,因此 while
循环终止。
因此两个ctrl+d
终止函数。单个 ctrl+z
也会终止程序。
情况是这样的,我有一段代码读取值,直到用户输入 Ctrl+D,但问题是我必须输入两次才能成功退出循环,我认为这与事实上,Ctrl+D 与缓冲区的剩余部分一起被插入到字符串中,因此没有注册为 EOF,只是字符串中的另一个字符,只有第二个 Ctrl+D 将被保存在一个 int 变量中,实际上停止了阅读循环。为了解决这个问题,我添加了 while(getchar() != '\n') 以查看它是否解决了问题,但没有结束。 这是我的代码:
typedef struct {
char nome[50];
int tempo;
int ncertas;
} equipa;
int ler_equipas(equipa *resultados)
{
int x, r1, r2 ,r3 ,r4, r11, r22, r33 ,r44, rcertas = 0, i = 0;
scanf ("%d %d %d %d", &r1, &r2, &r3, &r4);
while (scanf ("%s %d %d %d %d %d", resultados[i].nome, &resultados[i].tempo, &r11, &r22, &r33, &r44 ) == 6 )
{
i++;
if (r1 == r11) rcertas++;
if (r2 == r22) rcertas++;
if (r3 == r33) rcertas++;
if (r4 == r44) rcertas++;
resultados[i-1].ncertas = rcertas;
rcertas = 0;
while((x=getchar()) != '\n');
}
return i;
}
input/output结果如下(">>"表示输出):
1 2 3 4
>> correct answers are 1 2 3 4
Team1 234 1 2 3 4
>> values inputed for team time and answers: Team1 234 1 2 3 4
>> getchar value was (ASCII): 10
>> Next read:
Team2 400 1 3 2 4
>> values inputed for team time and answers: Team2 400 1 3 2 4
>> getchar value was (ASCII): 10
>> Next read:
>>
^D
^D
>> return i was reached
>> Number of teams: 2
>> Team "Team1" took 234 seconds and answered 4 questions correctly
>> Team "Team2" took 400 seconds and answered 2 questions correctly
我还没有试过这个。但我记得有同样的问题...
试试这个:How to clear input buffer in C?
while ((c = getchar()) != '\n' && c != EOF) { }
这是一种清除输入缓冲区的方法。
但这是在调用 scanf();
清除输入缓冲区之后。
此致
由于您正在使用 windows,因此您应该使用 ctrl+z
来模拟 EOF
见
当您使用 ctrl+d
时,第一个 ctrl+d
与初始 %s
匹配,因此扫描继续。
第二个 ctrl+d
将不匹配 %d
因此扫描终止。由于扫描的元素数不是 6,因此 while
循环终止。
因此两个ctrl+d
终止函数。单个 ctrl+z
也会终止程序。