Properly using sscanf

后端 未结 4 1143
星月不相逢
星月不相逢 2020-12-13 05:24

I am supposed to get an input line that can be in of any of the following formats:

  • There must be space between word 1 and word 2.
  • There must be a comm
4条回答
  •  感动是毒
    2020-12-13 06:03

    This is beyond the scope of scanf and friends, to be perfectly honest; in addition to the answers of "write your own simple parser", you could to invest in yacc to parse the grammer (the lexer is left as an exercise for the reader):

    line: oneword | twowords | threewords;
    oneword: word;
    twowords: word word;
    threewords: word word word;
    word: STRING;
    

    This may be overkill for you here, but if you ever need to parse even more than marginally complex formats, it's a lifesaver.

提交回复
热议问题