我如何读取c。使用scanf的空白? [英] How do I read white space using scanf in c?

查看:91
本文介绍了我如何读取c。使用scanf的空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我需要能够当两个空格连续发生识别

Problem: I need to be able identify when two whitespaces occur consecutively.

我已经阅读了以下几个问题:

I have read the following questions:

<一个href=\"http://stackoverflow.com/questions/2718595/how-to-read-a-string-from-a-n-delimited-file\">http://stackoverflow.com/questions/2718595/how-to-read-a-string-from-a-n-delimited-file

<一个href=\"http://stackoverflow.com/questions/2718819/how-to-read-scanf-with-spaces\">http://stackoverflow.com/questions/2718819/how-to-read-scanf-with-spaces

和我所知道的问题scanf函数: http://c-faq.com/stdio/scanfprobs.html

And I am aware of scanf problems: http://c-faq.com/stdio/scanfprobs.html

输入将在以下格式:

1 5 3 2  4 6 2  1 9  0

两个空格指示下一组数据的需要处理并与本身。该行的长度是未知的,各组中的号或整数是未知的。两个空格是将单独的下一个数据集最多的。

Two white spaces indicates that the next set of data needs to be handle and compared to itself. The length of the line is unknown and the number or integers in each group is unknown. Two whitespaces is the most that will separate the next data set.

虽然我可以使用fgets和各种内置函数来解决这个问题,我在点,其中在该点与scanf的解决该问题可能会更容易些。但是,如果不是这种情况,使用与fgets,strtok的和与atoi将完成大部分的工作,但我仍然需要确定在一行中的两个空格。

While I can use fgets and various built in functions to solve this problem, I am at the point where solving the problem with scanf at this point will likely be easier. However, if that's not the case, using fgets, strtok and atoi will do most of the job but I still need to identify two whitespaces in a row.

,直到一个非整数便被输入的下面将采取的整数。

The below will take integers until a non-integer is inputed.


while ( scanf ( "%d", &x ) == 1 )

我需要做的是阅读的空格,以及如果有两个连续的空格,我会计划做一些与下一组数据的不同。

What I need it do is read whitespaces as well and if there is two consecutive whitespaces I'll the program to do something different with the next set of data.

一旦我得到一个空白,我不知道怎么说:

And once I do get a white space I don't know how to say:


if ((input == "whitespace") && (previousInput == "whitespace"))
  ya da ya da
else (input == "whitespace")
  ya da ya da
else 
  ya da ya da

我AP preciate你的时间和感谢你的帮助。

I appreciate your time and thank you for your help.

教训:
虽然scanf的一个解决方案是由乔纳森·莱弗勒贴在下面,该解决方案是更直接与GETC位(通过要求内scanf函数的少成竹在胸,经常前pressions和char的方式)。现在回想起来更好常规EX pressions,scanf函数和char的知识将所取得的问题比较容易,当然,知道哪些功能可用,哪些人会一直从一开始就用最好的之一。

Lesson learned: While a solution for scanf is posted below by Jonathan Leffler, the solution was a bit more straightforward with getc (by way of requiring less intimate knowledge of the inner scanf, regular expressions and char). In retrospect better knowledge of regular expressions, scanf and char would of made the problem easier and of course knowing what functions are available and which one would have been the best one to use from the start.

推荐答案

GETC 与ungetc 是您的朋友

#include <stdio.h>

int main(void) {
  int ch, spaces, x;
  while (1) {
    spaces = 0;
    while (((ch = getc(stdin)) != EOF) && (ch == ' ')) spaces++;
    if (ch == EOF) break;
    ungetc(ch, stdin);
    if (scanf("%d", &x) != 1) break;
    printf("%d was preceded by %d spaces\n", x, spaces);
  }
  return 0;
}

http://ideone.com/xipm1

修改 Rahhhhhhhhh ...我上传的,作为C ++。这里是完全相同的事情,但现在 C99严格的http:// ideone .COM / mGeVk

Edit Rahhhhhhhhh ... I uploaded that as C++. Here's the exact same thing, but now C99 strict( http://ideone.com/mGeVk )

这篇关于我如何读取c。使用scanf的空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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