Reading a string with spaces with sscanf

前端 未结 5 928
终归单人心
终归单人心 2020-11-28 05:47

For a project I\'m trying to read an int and a string from a string. The only problem is sscanf() appears to break reading an %s when it sees a spa

5条回答
  •  一个人的身影
    2020-11-28 06:17

    You want the %c conversion specifier, which just reads a sequence of characters without special handling for whitespace.

    Note that you need to fill the buffer with zeroes first, because the %c specifier doesn't write a nul-terminator. You also need to specify the number of characters to read (otherwise it defaults to only 1):

    memset(buffer, 0, 200);
    sscanf("19 cool kid", "%d %199c", &age, buffer);
    

提交回复
热议问题