简体   繁体   中英

Format Specifier for sscanf: %{format%}

I've only seen a couple of references to the format in the title, and no good examples. The definition I saw was this:

%{format%} Repeatedly matches the format specifier format as many times as possible, and gives an array of arrays with the results.

Does anyone have a good example of how to use this? What do you need to pass in to receive the results?

It seems that you got this quote from docs.roxen/pike , which is not standard C.

Pike is an object-oriented programming language with a syntax similar to Java and C . It is not C!


In order to learn about sscanf() , uou should check the reference , which mentions:

format : C string that contains a format string that follows the same specifications as format in scanf (see scanf for details).

and also provides an example. Another example would be:

#include <stdio.h>

int main () {
   int day, year;
   char month[10], date[15] = "29 May 1453";

   int items_read = sscanf(date, "%d %s %d", &day, month, &year);

   printf("Constantinople fell in %d %s %d. sscanf() Read %d items.\n", day, month, year, items_read);

   return 0;
}

Output:

Constantinople fell in 29 May 1453. sscanf() Read 3 items.

Here sscanf() expects to date as the source, and will match the format to the parameters after it. It will attempt to match the string date to an integer, followed by a space, a string, a space and an integer.

As you can see, date has an integer (29), then a space, then a string ("May"), then a space and then an integer (1453), thus it matches the format perfectly. It will then assign these values to the corresponding variables.

Note how the return value of the method, on success, returns the number of items in the argument list successfully filled.

This appears to be exclusive to Pike, which is not quite C++.

Pike is an interpreted, object-oriented programming language. It looks a bit like C and C++, but it is much easier to learn and use. It can be used for small scripts as well as for large programs.

http://docs.roxen.com/pike/7.0/tutorial/introduction/index.xml

http://docs.roxen.com/pike/7.0/tutorial/strings/sscanf.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM