来自 stdin 的 fgets 问题 [C]
我正在编写一个处理文件的程序。 我需要能够将数据作为结构输入,并最终将其读出。 我目前遇到的问题是这段代码:
typedef struct {
char* name;
.....
}employeeRecord;
employeeRecord record;
char name[50];
if(choice == 1)
{
/*Name*/
printf("\nEnter the name:");
fgets(name,50,stdin);
record.nameLength = strlen(name) -1;
record.name = malloc(sizeof(char)*record.nameLength);
strcpy(record.name,name);
/*Other data, similar format...*/
例如,如果我想要姓名地址和电话号码,并连续询问每个号码(因此地址与上面几乎相同,除了用地址替换“姓名”),我发现它跳过了输入。我的意思是,我没有机会输入它。输出实际上是 输入姓名: 输入地址:(这里是提示我输入的地方)
I'm writing a program that works with files.
I need to be able to input data as structures, and eventually read it out.
The problem i have at the moment is with this code:
typedef struct {
char* name;
.....
}employeeRecord;
employeeRecord record;
char name[50];
if(choice == 1)
{
/*Name*/
printf("\nEnter the name:");
fgets(name,50,stdin);
record.nameLength = strlen(name) -1;
record.name = malloc(sizeof(char)*record.nameLength);
strcpy(record.name,name);
/*Other data, similar format...*/
If i want for example, name address and phone number, and ask for each in a row (so address is pretty much identical to above except replacing 'name' with address), i find it skips the input. What i mean is, I am given no chance to input it. The output is actually
Enter the name:
Enter the address: (and here is where it prompts me for input)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
换行符仍在
stdin
中,来自之前对未从输入读取换行符的函数的调用。通过读取直到读出换行符来清除stdin
- 而不是像其他人建议的那样通过刷新stdin
来清除。编辑:谢谢 Alok 的更正!
The newline is still in
stdin
from a prior call to a function that didn't read a newline from input. Clearstdin
by reading until you've read out the newline -- not by flushingstdin
as others have suggested.EDIT: Thanks Alok, for the correction!
我尝试了您的代码,但无法重现该问题。下面的代码按照您期望的方式工作,它提示输入名称,等待您输入名称,然后提示输入地址,等等。
我想知道您是否不需要读取 stdin 并将其清空您提示需要更多输入吗?
顺便说一句,您想将 strlen(name) 加 1,而不是减 1。或者,如果您希望将 name 存储在记录中而不以空终止,则需要使用 memcpy 将字符串复制到记录中,而不是 strcpy。
编辑:
我从评论中看到您正在使用
scanf
读取选择值,这会在输入缓冲区中留下一个 \n ,然后由您的第一个fgets
拾取该值称呼。您应该做的是使用 fgets 读取选择行,然后使用 sscanf 解析输入中的值。像这样,应该使刷新标准输入的整个问题变得毫无意义。
I tried your code and can't reproduce the problem. The following code works just the way you would expect, it prompts for the name, wait for you to type the name, then prompts for the address, etc.
I'm wondering if you don't need to read stdin and empty it before you prompt for more input?
Incidently, you want to add 1 to strlen(name), not subtract 1. or, if you want name stored in your record without a terminating null, then you need to use memcpy to copy the string into your record, not strcpy.
Edit:
I see from comments that you are using
scanf
to read the choice value, this is leaving a \n in the input buffer which is then picked up by your firstfgets
call. What you should do instead is to use fgets to read in the choice line, and then sscanf to parse the value out of the input. like thisthat should make the whole issue of flushing stdin moot.
在调用
fgets
读取名称之前,您可能使用scanf
读取choice
。scanf
可能在stdin
中留下了换行符,您的代码将其误认为是空名称输入。如果确实如此,请尽量不要使用scanf
(使用fgets
检索choice
并使用atoi
来检索转换为int
或strcmp
以与“1\n”等进行比较)。否则,代码应该可以工作,并进行以下修改,以考虑到fgets
还将终止换行符读入缓冲区(您可能希望将其删除)的事实:You probably used
scanf
to readchoice
before callingfgets
to read the name.scanf
may have left a newline instdin
which your code mistakes for an empty name input. If that is indeed the case, try not to usescanf
(usefgets
to retrievechoice
and useatoi
to conver toint
orstrcmp
to compare against "1\n" etc.). The code should otherwise work, with the below modifications to account for the fact thatfgets
also reads the terminating newline into the buffer (which you probably want stripped):