与fgets不会等待键盘输入 [英] fgets doesn't wait for the keyboard input

查看:263
本文介绍了与fgets不会等待键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要阅读疗法用户的键盘输入两个字符串,这是code我尝试:

 字符nomFichier [50],EMP [100],empEtNomFichier [150];
的printf(\\ nDonner乐NOM杜fichier:);
与fgets(nomFichier,sizeof的nomFichier,标准输入);
的printf(\\ nDonner欧莱雅进驻杜fichier:);
与fgets(EMP,sizeof的EMP,标准输入)
sprintf的(empEtNomFichier,%S /%S,EMP,nomFichier);

问题是,当我运行这个code,程序不等待第一个与fgets键盘输入(),广告这是怎样的程序会:

 唐纳乐NOM杜fichier:
唐纳L'进驻杜fichier:/家庭/ EE /桌面
/家庭/ EE /桌面


解决方案

问题是真的与fgets()读取结尾的换行符到字符串。除非缓冲区已满,或EOF是不换行遇到了返回的字符串中的最后一个字符将是'\\ n'。

讨论通过去掉是:

  N = strlen的(nomFichier);
如果(正大于0&放大器;&放大器; nomFichier [N-1] ==的'\\ n'){nomFichier [N-1] = 0; }

...并为如果块内的EMP字符串做同样的。也有方法来使用的strtok用的'\\ n'作为分隔符,或者使用的sscanf有XX%[^ \\ n]的%* C格式,再加上我相信其他人有不同的喜爱解决方案。 (xx是最大字符串长度,十进制,在scanf函数的格式。)

顺便说一下,其它问题不能可移植性由fflush解决,因为fflush不一定做任何事情。 GNU C实现,例如,治疗fflush(标准输入)作为空操作。

问题是最有可能的原因是前scanf()的读取值,但没看过换行符。即面向令牌输入(scanf函数/的fscanf)和面向线路输入(与fgets)之间切换程序必须ppared对付剩下的线$ P $。

最快的解决方法是:

  scanf函数(%* [^ \\ n]的%* C); / *丢弃直至并包括在未来的'\\ n'字符* /

...或更直接的:

  INT℃;
而((三==的getchar())= EOF和放大器;!&安培;!C ='\\ n'){}

无论哪种方式,我把在一个静态功能,让优化器决定是否内联与否。

您需要做的,每次你scanf()的切换与fgets()输入。

I want to read two strings from ther user's keyboard input, this is the code I tried :

char nomFichier[50], emp[100], empEtNomFichier[150];
printf("\nDonner le nom du fichier : ");
fgets(nomFichier, sizeof nomFichier, stdin);
printf("\nDonner l'emplacement du fichier : ");
fgets(emp, sizeof emp, stdin)
sprintf(empEtNomFichier, "%s/%s", emp, nomFichier);

The problem is when I run this code, the program doesn't wait for the keyboard input for the first fgets(), ad this is how the program looks :

Donner le nom du fichier : 
Donner l'emplacement du fichier : /home/ee/Desktop
/home/ee/Desktop

解决方案

The problem is really that fgets() reads the terminating newline into the string. Unless the buffer fills up, or EOF is encountered without a newline, the last character in the returned string will be '\n'.

Get rid of that by:

n = strlen(nomFichier);
if (n>0 && nomFichier[n-1]=='\n') { nomFichier[n-1] = 0; }

...and do the same for the emp string inside the if block. There are also ways to use strtok with '\n' as a delimiter, or use sscanf with a "%xx[^\n]%*c" format, plus I'm sure others have different favorite solutions. ("xx" is the maximum string length, in decimal, in that scanf format.)

By the way, the other problem can't be portably addressed by fflush because fflush doesn't necessarily do anything. GNU C implementations, for example, treat fflush(stdin) as a no-operation.

The problem is most likely due to a prior scanf() that read a value, but didn't read the newline. Programs that switch between token-oriented input (scanf/fscanf) and line-oriented input (fgets) must be prepared to deal with the leftover line.

The quickest fix is:

scanf("%*[^\n]%*c"); /* discard up to and including the next '\n' character */

...or the more direct:

int c;
while ( (c==getchar()) != EOF && c != '\n') { }

Either way, I'd put that in a static function and let the optimizer decide whether to inline it or not.

You need to do that each time you switch from scanf() to fgets() input.

这篇关于与fgets不会等待键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆