解析输入在C scanf函数 [英] Parsing input with scanf in C

查看:201
本文介绍了解析输入在C scanf函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有很多问题试图找出如何使用 scanf()的。它似乎做工精细用整数,是相当直截了当 scanf函数(%d个,&安培; I)。

在哪里我遇到的问题是使用 scanf()的在循环试图读取输入。例如:

  {做
  的printf(反数:%d:,计数器);
  scanf函数(%C%C%D,&安培;命令,和放大器; preFIX,&安培;输入);
}而(命令='Q'!);


  1. 当我像çP101 一个有效的结构输入输入,它似乎促使我前再次循环。这似乎与一个甚至发生:

      scanf函数(%C,和C)

    在一个while循环。再次提示我之前它会做循环两次。是什么使得它循环两次,我该如何阻止它?


  2. 当我在编程不会有另一个字符或数字,如,pressing进入输入量少进入似乎提示我进入更多。我如何获得 scanf()的来处理单,双字条目?



解决方案

当你输入çP101 程序实际收到ÇP101 \\ n 。大部分的转换说明的跳过前导空白包括换行,但%C 没有。第一次周围的一切,不外了 \\ n 被读取时,周围的第二次\\ n是读入命令 C 读入 preFIX P 留下这不是一个号码,以便转换失败和 P101 \\ n 被保留在流。下一次 P 被存储到命令, 1 存入preFIX和 1 (从剩下的 01 )存入的输入\\ n 仍然在流,用于下一次。您可以通过格式字符串,这将跳过任何前导空格,包括换行的开头放一个空间,解决此问题。

一个类似的事情也发生在第二种情况下,当你输入问:\\ n 输入到流中,第一次围绕被读取时,第二次 \\ n 被读出,仅在第三个电话是继阅读,您可以通过在格式字符串的开头添加一个空格字符再次避免此问题。

有一个更好的方式来做到这一点是使用类似与fgets()来处理在一个时间线,然后使用的sscanf()做解析。

I've been having a lot of problems trying to figure out how to use scanf(). It seems to work fine with integers, being fairly straight forward scanf("%d", &i).

Where I am running into issues is using scanf() in loops trying to read input. For example:

do {
  printf("counter: %d: ", counter);
  scanf("%c %c%d", &command, &prefix, &input);
} while (command != 'q');

  1. When I enter in a validly structured input like c P101, it seems to loop again before prompting me. This seems to happen even with a single:

    scanf("%c", &c) 
    

    in a while loop. It'll do the loop twice before prompting me again. What is making it loop twice, and how do I stop it?

  2. When I enter in less amount of input that programmatically wouldn't have another character or number such as q, pressing enter seems to prompt me to enter more. How do I get scanf() to process both single and double character entries?

解决方案

When you enter "c P101" the program actually receives "c P101\n". Most of the conversion specifiers skip leading whitespace including newlines but %c does not. The first time around everything up til the "\n" is read, the second time around the "\n" is read into command, "c" is read into prefix, and "P" is left which is not a number so the conversion fails and "P101\n" is left on the stream. The next time "P" is stored into command, "1" is stored into prefix, and 1 (from the remaining "01") is stored into input with the "\n" still on the stream for next time. You can fix this issue by putting a space at the beginning of the format string which will skip any leading whitespace including newlines.

A similiar thing is happening for the second case, when you enter "q", "q\n" is entered into the stream, the first time around the "q" is read, the second time the "\n" is read, only on the third call is the second "q" read, you can avoid the problem again by adding a space character at the beginning of the format string.

A better way to do this would be to use something like fgets() to process a line at a time and then use sscanf() to do the parsing.

这篇关于解析输入在C scanf函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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