【问题标题】:Ignoring commas and dots using fscanf()使用 fscanf() 忽略逗号和点
【发布时间】:2011-12-17 22:51:07
【问题描述】:

我正在尝试使用 fscanf() 读取不带逗号和点的字符串。

示例输入:

“女贞路四号的德思礼夫妇自豪地说他们完全正常,非常感谢。”

我想每次都读“先生”、“夫人”和“德思礼”,例如

我尝试了几种使用可选参数的方法,但都失败了。如何使用 fscanf() 忽略逗号和点?

【问题讨论】:

    标签: arguments scanf


    【解决方案1】:

    您可以使用the regex feature of sscanf 来执行此操作。

    #include <stdio.h>
    
    
    int main()
    {
      char *str = "Mr. Fiddle Tim went to the mall. Mr. Kurdt was there. Mrs. Love was there also. "
        "They said hi and ate ice cream together." ;
    
      char res[800] = { 0 };
      sscanf( str, "%800[^.,]", res ) ;
      puts( str ) ;
    }
    

    要继续,您需要使用sscanf(或fscanf)的返回值来确定匹配的字符数。我把它留给你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 2010-09-19
      相关资源
      最近更新 更多