如何使用与fgets和sscanf在循环整数 [英] how to use fgets and sscanf for integers in loop

查看:335
本文介绍了如何使用与fgets和sscanf在循环整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初​​学者用C在这里。我想运行一个循环,字符串和整数输入到一个结构各个领域。当系统提示姓氏,用户可以preSS没有其他输入输入和循环应该结束了。

Beginner with C here. I am trying to run a loop where strings and ints are entered into various fields of a struct. When prompted for a 'last name', the user can press enter with no other input and the loop should end.

问题是,这个code,循环不结束(姓和名条目请求一起在同一线路上运行)和工资的价值永远是错的(0或一些大的数字)

The problem is that with this code, the loop doesnt end (last name and first name entry requests run together on the same line) and the value for salary always comes out wrong (0 or some large number)

while (employee_num <= 2)
{
    printf("Enter last name ");
    fgets(employee[employee_num].last_name, sizeof(employee[employee_num].last_name), stdin);                   

    if(strlen(employee[employee_num].last_name) == 0)
        break;

    printf("Enter first name ");
    fgets(employee[employee_num].first_name, sizeof(employee[employee_num].first_name), stdin);

    printf("Enter title ");
    fgets(employee[employee_num].title, sizeof(employee[employee_num].title), stdin);

    printf("Enter salary ");
    fgets(strng_buffer, 1, stdin);
    sscanf(strng_buffer, "%d", &employee[employee_num].salary);     
    ++employee_num;
    getchar();
}

如果我试试这个code,而不是,我可以通过它在第一次运行后正常退出循环,但之后不会退出(由pressing在最后一个名称部分输入 - 也许是\\ n我似乎无法清除):

If I try this code instead, I am able to exit the loop properly after the first run through it, but cannot exit after that (by pressing enter at the last name portion - perhaps a \n I cant seem to clear?):

char strng_buffer[16];
while (employee_num <= 5)
{
    printf("Enter last name ");
    fgets(strng_buffer, sizeof(strng_buffer), stdin);                   
    sscanf(strng_buffer, "%s", employee[employee_num].last_name);       

    if(strlen(employee[employee_num].last_name) == 0)
        break;

    printf("Enter first name ");
    fgets(strng_buffer, sizeof(strng_buffer), stdin);
    sscanf(strng_buffer, "%s", employee[employee_num].first_name);


    printf("Enter title ");
    fgets(strng_buffer, sizeof(strng_buffer), stdin);
    sscanf(strng_buffer, "%s", employee[employee_num].title);

    printf("Enter salary ");
    scanf("%d", &employee[employee_num].salary);        
    ++employee_num;
    getchar();
}

我很好奇,如何使这项工作打算什么最好的做法是这样的条目(即使用的sscanf的,与fgets等)

I am curious as to how to make this work as intended and what best practice would be for entries like this (ie use of sscanf, fgets, etc)

在此先感谢!

推荐答案

假设作者Abhijit提及该修补程序,为什么改造第一到第二个?您是否知道第二行为不同为先,因为加了的sscanf 的?如果你的目的是缩短第一,第二似乎很笨重。 的sscanf 来的情况下,为什么不通过声明结构员工* E =员工+ employee_num缩短第一>和使用重复,而不是员工[employee_num]

Assuming the fix mentioned by Abhijit, why transform the first into the second? Are you aware that the second behaves differently to the first, because of the addition of sscanf? If your intention was to shorten the first, the second seems quite bulky. Rather than adding sscanf to the situation, why not shorten the first by declaring a struct employee *e = employee + employee_num; and using that repetitively, instead of employee[employee_num]?

一个最佳实践关于与fgets 是要检查它的返回值。你猜 与fgets 可能会返回,如果遇到 EOF ?你猜 与fgets 如果成功将返回?

One "best practise" regarding fgets is to check it's return value. What do you suppose fgets might return, if it encounters EOF? What do you suppose fgets would return if it's successful?

一个最佳实践关于 scanf函数是要检查它的返回值。在关于 scanf函数的返回值,我建议您阅读的这个 scanf函数手动仔细,回答下列问题:

One "best practise" regarding scanf is to check it's return value. In regards to the return value of scanf, I suggest reading this scanf manual carefully and answering the following questions:


  1. INT X = scanf函数(%d个,&安培;员工[employee_num] .salary); 你猜 X FUBAR \\ N输入?

  2. 如果你想在'F'FUBAR \\ N将何去何从?

  3. 如果是与ungetc 倒是返回到标准输入,什么将你的下一个员工的姓是什么?

  4. INT X = scanf函数(%d个,&安培;员工[employee_num] .salary); 你猜 X 将是,如果我在Windows和preSS CTRL + Z运行此code发送 EOF 标准输入

  5. INT X = scanf函数(%D,&安培; Y,放大器; Z); 你所期望的 X 是,presuming scanf函数成功地把值放入两个变量ž

  1. int x = scanf("%d", &employee[employee_num].salary); What do you suppose x will be if I enter "fubar\n" as input?
  2. Where do you suppose the 'f' from "fubar\n" will go?
  3. If it's ungetc'd back onto stdin, what would your next employee's last name be?
  4. int x = scanf("%d", &employee[employee_num].salary); What do you suppose x will be if I run this code on Windows and press CTRL+Z to send EOF to stdin?
  5. int x = scanf("%d %d", &y, &z); What would you expect x to be, presuming scanf successfully puts values into the two variables y and z?

P.S。 EOF 可以通过标准输入在Windows中按CTRL + Z按CTRL + D发送,而在Linux和朋友,在除了使用管道和重定向重定向从其他程序和文件输入。

P.S. EOF can be sent through stdin in Windows by CTRL+Z, and in Linux and friends by CTRL+D, in addition to using pipes and redirection to redirect input from other programs and files.

这篇关于如何使用与fgets和sscanf在循环整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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