【问题标题】:Scanset behaviour in scanf in CC中scanf中的扫描集行为
【发布时间】:2012-07-24 13:41:17
【问题描述】:

我尝试在 scanf 中使用 scanset 做一些事情,但卡在某个地方。

当我写作时

char s1[250];
scanf("%[A-Z]",s1);

input : AHJHkiuy
Output: AHJH

有了这个,

scanf("%[^\n]",s1);

input: abcd ABCD hie
output: abcd ABCD hie       /*that is reading white space also (till \n) */

现在我的问题是,如果我输入如下:

ABCDahaj ahajABCD ajak12347ab

并希望输出为:

ABCDahaj ahajABCD ajak

那么格式字符串应该怎么写呢?也就是这个scanset应该怎么用?

【问题讨论】:

  • 您是否仔细在 Linux 或 Unix 系统上阅读了man scanf 的输出??

标签: c scanf


【解决方案1】:

您可以稍微扩展您的示例并实现您的目标。

scanf("%[A-Za-z ]", s1);

【讨论】:

    【解决方案2】:

    另一种方法是:

    scanf("%[^0-9]", s1); /* Scans everything until a digit */
    

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多