SSCANF Usage Detailed

Source: Internet
Author: User
reproduced from: http://baike.baidu.com/view/1364018.htm Name:SSCANF ()-Reads data from a string that matches the specified format.   function prototypes: int sscanf (const char *, const char *, ...);   int sscanf (const char *buffer,const char *format,[argument] ...); Buffer stored data Format format Control string argument Optional set string sscanf will read the data from the buffer and write back the data according to the argument settings.
header file:#include <stdio.h>
return Value:
Success returns the number of parameters, failure returns 0, and the reason for the error is in errno.
Description:
SSCANF is similar to scanf and is used for input, except that the keyboard (stdin) is the input source, with a fixed string as the input source. The first parameter can be one or more {%[*] [width] [{h | l | I64 | L}]type | ' ' | ' t ' | ' \ n ' | Non-% symbol} NOTE: 1, * can also be used in the format, (that is,%*d and%*s) with an asterisk (*) indicates that skipping this data is not read.   (that is, do not read this data into the parameter) 2, {a|b|c} means that a,b,c select one, [d], to indicate that you can have D or not d.   3. Width represents the reading breadth. 4, {h | l | I64 |   L}: The size of the parameter, usually H represents a single-byte Size,i 2 byte size,l represents 4 byte size (double exception), and L64 represents 8 byte size.   5, type: This is a lot of, is%s,%d and so on. 6, Special:%*[width] [{h | l | I64 | L}]type indicates that a filter that satisfies the condition does not return 0 to the target parameter failure, otherwise the number of formatted parameters is returned
support for collection operations:%[a-z] that matches any character in A to Z, greed (as much as possible)%[ab ' matches A, B, ' one, greedy%[^a] matches any character not a, and stops reading, greed
Example:
1. Common usage.   Char buf[512];   SSCANF ("123456", "%s", buf);//Here buf is the array name, which means to deposit 123456 in the buf form%s.   printf ("%s\n", buf); The results are: 123456 2. The string that takes the specified length.   In the following example, take a string with a maximum length of 4 bytes.   SSCANF ("123456", "%4s", buf);   printf ("%s\n", buf); The results are: 1234 3. The string to take to the specified character.   In the following example, the string is encountered as a space.   SSCANF ("123456 Abcdedf", "%[^]", buf);   printf ("%s\n", buf); The results are: 123456 4. Takes a string containing only the specified character set.   In the following example, take a string that contains only 1 to 9 and lowercase letters.   SSCANF ("123456abcdedfBCDEF", "%[1-9a-z]", buf);   printf ("%s\n", buf);   The result is: 123456ABCDEDF when input: sscanf ("123456abcdedfBCDEF", "%[1-9a-z]", buf);   printf ("%s\n", buf); The results are: 123456 5. The string to take to the specified character set.   In the following example, take the string that is encountered in uppercase letters.   SSCANF ("123456abcdedfBCDEF", "%[^a-z]", buf);   printf ("%s\n", buf); The result is: 123456ABCDEDF 6, given a string iios/12ddwdff@122, gets/And the string between @, first filter out "iios/", and then send a string of non ' @ ' to buf sscanf ("iios/12ddwdff@12   2 ","%*[^/]/%[^@] ", buf);   printf ("%s\n", buf); The result is: 12DDWDFF 7, given a string "Hello, World", just keep world.   (Note: "," After a space,%s to stop the space, plus * is to ignore the first read the string) sscanf ("Hello, World", "%*s%s", buf); Printf("%s\n", buf);   The result: World%*s indicates that the first matching%s was filtered, that is, Hello was filtered and null if no spaces were obtained.   SSCANF functions are similar to regular expressions but do not have a strong regular expression, so it is recommended that you use regular expressions for more complex string processing.   -------------------------------------------------------use it to separate strings like this 2006:03:18:int A, B, C; /*SSCANF ("2006:03:18", "%d:%d:%d", A, B, c);   and 2006:03:18-2006:04:18:char sztime1[16] = "", sztime2[16] = "";   SSCANF ("2006:03:18-2006:04:18", "%s-%s", sztime1, sztime2);   But later, I needed to deal with 2006:03:18-2006:04:18 just canceled the '-' on both sides of the space, but broke the%s of the string definition. I need to redesign a function to handle this situation. It's not complicated, but in order for all the code to have a unified style, I need to change a lot of places and replace the existing sscanf with my own split function.   I thought I was sure I needed to do this, and I went to sleep with a strong dissatisfaction with sscanf, and woke up and found that it wasn't necessary. In Format-type, there is a type field such as%[].   If you read a string that is not separated by a space, you can use%[]. %[] is similar to a regular expression.   [A-z] means reading all the characters in a-Z, [^a-z] means reading all characters except A-Z.   So the problem is solved: sscanf ("2006:03:18-2006:04:18", "%[0-9,:]-%[0-9,:", sztime1, sztime2); -------not the author of this paragraph---------------------------------------------------------------------------------------------------sscanf ("2006:03:18-2006:04:18", "%[^-]-%s",   SZTIME1,SZTIME2); Description:%[^-] matches to '-' default to sztime1 string ' 2006:03:18 ' plus spaces, so%s will enter " -2006:04:18" by default to sztime2 this is the reason for%s plus '-'.   The output is the same as the author, but is easier to understand and readable. ----------------------------------------------------------------------------------------------------End---------------- -In the Softmse (Jake) question paste HTTP://COMMUNITY.CSD (remove me) n.n (remove me) et/expert/topic/4843/4843294.xml?temp=.4321558,   Give a very cool sscanf use case, and then through learning, found sscanf awesome, now do a summary.   Original question: iios/12ddwdff@122 get/And the string between @ How do you have a function in C program?   Code: #include <stdio.h> int main () {const char* s = "iios/12ddwdff@122";   Char buf[20];   SSCANF (S, "%*[^/]/%[^@]", buf);   printf ("%s\n", buf);   return 0; The result is: 12DDWDFF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.