从标准输入问题与fgets [C] [英] fgets from stdin problems [C]

查看:284
本文介绍了从标准输入问题与fgets [C]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序,处理文件工作。
我需要能够输入数据作为结构,并最终读出。
我目前所面对的问题是这个code:

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...*/

如果我想,例如名称地址和电话号码,并要求每一个行(所以地址是pretty多少等同于上面只是用'名'与地址),我觉得它跳过输入。我的意思是,我给没有机会将其输入。输出实际上是
输入名称:
输入地址:(这里是它提示我输入)

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)

推荐答案

我想你的code和无法重现该问题。下面code工程只是你期望的方式,它会提示名称,等你键入名称,然后会提示输入地址等。

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?

typedef struct {
    char* name;
    char* address;
}employeeRecord;

int readrecord(employeeRecord &record)
{
   char name[50];
   char address[100];

   printf("\nenter the name:");
   fgets(name, sizeof(name), stdin);
   record.nameLength = strlen(name) + 1;
   record.name = malloc(sizeof(char)*record.nameLength);
   strcpy(record.name,name);

   printf("\nenter the address:");
   fgets(address, sizeof(address), stdin);

   ...    
}

顺便说一句,你要加1的strlen(名称),而不是减1,或者,如果你想要的名称保存在您的记录没有一个空结尾,那么你需要使用的memcpy到字符串复制到你的记录,不strcpy的。

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.

我从您正在使用 scanf函数阅读选择的价值的意见看,这是留在输入缓冲区的\\ n然后由你的第一次拿起与fgets 电话。你应该做的,而不是是使用与fgets在选择线上阅读,然后sscanf的解析值了投入。像这样

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 first fgets 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 this

int choice;
char temp[50];
fgets(temp, sizeof(temp), stdin);
sscanf(temp, "%d", &choice);

这应使冲洗标准输入无实际意义的整个问题。

that should make the whole issue of flushing stdin moot.

这篇关于从标准输入问题与fgets [C]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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