C language function sscanf () usage - reading from a string consistent with the specified data format (rpm)

C language function sscanf () usage

sscanf () - read data into the specified format match from a string.
  Function Prototype:
  int sscanf (STR String, String FMT, Mixed var1, var2 ... Mixed);
  int Scanf (const char * format [, argument ] ...);
  Description:
  sscanf scanf and similar, are used for input, but the latter to a screen (stdin) as the input source, the former with a fixed string as the input source.
  Wherein the format may be one or more {% [*] [width] [{h | l | I64 | L}] type | '' | '\ t' | '\ n' | } sign non-%
  Notes:
  1 * format can also be used, (i.e.,% * d and% * s) plus an asterisk (*) indicates skip this data is not read. (this data is not read into the parameter)
  2, {a | b | c} represent a, b, c select one, [d], mean that there may be no d d.
  3, width represents the reading width.
  4, {h | l | I64 | L}: size parameter, generally h represents a single byte size, I represents a 2-byte size, L represents a 4-byte size (double the exception), L64 represents 8-byte size.
  5, type: This lot is% s,% d and the like.
  6, in particular:% * [width] [{ h | l | I64 | L}] type represents a number satisfying the condition are filtered out and are not written to the target parameter value
  supported collective operation:
  % [az] matches a to z represent any character, greedy (matches as much)
  % [aB '] matches a, B,' in one, greedy
  % [^ a] non-matches any character of a , greedy
Note: read character string is an empty string, sscanf function does not change the value to be read into a character string.

Examples:
  1. common usage.
  buf char [512] =;
  sscanf ( "123456", "% S", buf);
  the printf ( "% S \ n-", buf);
  results: 123456
  2. Take the string length specified. As the following example, taking the maximum length of 4 bytes string.
  sscanf ( "123456", "% 4S", buf);
  the printf ( "% S \ n-", buf);
  result: 1234
  3. Take up to the specified character string. As the following example, the space taken up encountered string.
  sscanf ( "123456 abcdedf", "% [^]", buf);
  the printf ( "% S \ n-", buf);
  results: 123456
  4. Take strings containing only the specified character set. As the following example, take 1-9 and contains only lowercase string.
  sscanf ( "123456abcdedfBCDEF", "% [1-9a-z]", buf);


  5. Take the string until the specified character set. As the following example, we encounter a string taken up capitalization.
  sscanf ( "123456abcdedfBCDEF", "% [^ AZ]", buf);
  the printf ( "% S \ n-", buf);
  results: 123456abcdedf
  . 6, given a string iios / 12DDWDFF @ 122, acquisition / @ and between strings, first "iios /" filtered out, then the non '@' to the contents of the string in buf
  sscanf ( "iios / 12DDWDFF @ 122 ", "% * [^ /] /% [^ @] ", buf);
  the printf ("% S \ n-; ", buf)
  results: 12DDWDFF
  a space after the": 7, given a string "" hello, world ", retaining only the world (note." )
  sscanf ( "Hello, World", "*% S% s", buf);
  the printf ( "% s \ n-", buf);
  results: World
  % * S denotes a matched filtered to% s out that hello is filtered
  if there is no space, the result is NULL.
  sscanf function is very similar to regular expressions, but not powerful regular expressions, so if the string handling for more complex, it is recommended to use regular expressions.


  From the above representation str, enter numbers to x, it is 32700
  years ago, I thought c do not own split string function, but then I found sscanf; All along, I thought sscanf only a space defined strings, and now I find I'm wrong.
  sscanf is a run-time function prototype is simple:
  int sscanf (
  const char * Buffer,
  const char * format [,
  argument] ...
  );
  it is reflected in the powerful support for the format.
  I formerly used to separate a string like this 2006: 03: 18 is:
  int A, B, C;
  sscanf ( "2006: 03: 18 is", "% D:% D: D%", A, B, C );
  and 2006: 03: 18 is - 2006: 04: 18 is:
  char sztime1 [16] = "", sztime2 [16] = "";
  sscanf ( "2006: 03: 18 is - 2006: 04: 18 is", "% s -% s ", sztime1, sztime2);
  but then, I need to handle 2006: 03: 18-2006: 04:18
  just canceled '-' both sides of the space, but broke the% s definition of string.
  I need to re-design a function to handle such a situation? This is not complicated, but to make all of the code has a unified style, I need to change a lot of places, to replace the existing sscanf into my own partition function. I think I definitely need to do this, and accompanied by a strong dissatisfaction and sscanf of sleep; woke up and discovered that in fact do not have to.
  format-type% had [] Such a type field. If the read character string is not separated by spaces, then you can use% [].
  % [] Is similar to a regular expression. [az] denotes read all the characters az, [^ az] means to read all the characters except az.
  So the problem will be solved:
  sscanf ( "2006: 03: 18 - 2006: 04: 18", "% [0-9 ,:] -% [,: 0-9]", sztime1, sztime2);

 

Transfer: https://www.cnblogs.com/lyq105/archive/2009/11/28/1612677.html

Guess you like

Origin www.cnblogs.com/zl1991/p/11579442.html