C Language Function Library learning ~sscanf~ formatting input

Source: Internet
Author: User
Tags sprintf uppercase letter

---restore content starts---

It's been hit today, isn't it? By Zheng Light of the ACM teacher to my college guidance arranged a small game,, we actually have or lost to a freshman novice,, Hey, love,,, so still want to pay attention to the basic programming ability training, now I began to learn the format of the input, very complex but very effective ...

SSCANF ()-Reads data in a string that matches the specified format.

Int sscanf (String str, String fmt, mixed var1, mixed var2 ...);
int scanf (const char *format [, argument] ...);

SSCANF is similar to scanf, which is used for input, except that the latter takes a screen (stdin) as the input source, the former with a fixed string as the input source.

There are several special symbolic representations of the parameters.

1, * can also be used in the format, (that is,%*d and%*s) with an asterisk (*) indicates that skipping this data is not read in. (that is, do not read this data into the parameters)
2. {A|b|c} indicates that a a,b,c is selected, [d], indicating that either D or D may not be available.
3. Width indicates read widths.
4. {H | l | I64 | L}: The size of the parameter, usually h represents a single-byte size,i that represents a 4-byte size (double exception) for a 2-byte size,l, and L64 represents a 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 the condition is filtered out and no value is written to the target parameter
Support for collection operations:
%[a-z] to match any character in a to Z, greed (as many matches as possible)
%[ab ' matches a, B, ' one member, greed
%[^a] matches any character not a, greed

We can look at the test data.

#include <stdio.h>
int main ()
{
Char buf[10];
SSCANF ("Hello, World", "%*s%s", buf);//This takes into account that the%s is at the end of the space, so the first%s is ignored. SSCANF ("Hello,\\nworld", "%*s%s", buf); it's the same with \\n here.
printf ("%s\n", buf);
}

SSCANF ("Hello, World", "%5s", buf); Get the first five characters

Of course, you would also like to be able to make the number as a word designators into the array we take a look at,

#include <stdio.h>
#include <string.h>
int main ()
{
Char buf1[100],buf2[100];
int A;
Double b;
scanf ("%d", &a);
scanf ("%lf", &b);//test if the decimal can go
Itoa (a,buf,10); This function can also implement a numeric to character, 10 for the binary
sprintf (BUF1, "%d", a);
sprintf (Buf2, "%lf", b);//Note that he's going to enter the number.
printf ("character-type int:%s\n", buf1);//Here the direct output of a is just as a character
printf ("Doubl of character type:%s\n", buf2);//Ibid.
printf ("int length:%d\n", strlen (BUF1));//Can be used to calculate the number of bits
printf ("Doubl Length:%d\n", strlen (BUF2));//length is a point
}

3. The string to take to the specified character. As in the following example, the string is encountered until the space is met.
SSCANF ("123456 Abcdedf", "%[^]", buf); "%[^" There is a space after this ^
printf ("%s\n", buf);
The result is: 123456


4. Take a string that contains only the specified character set. As 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

Here the sscanf is for a string of characters found in accordance with the% "1-9a-z" directly into the buf, and then jump out of the statement, so in this statement as long as the encounter is not recognized, such as e will directly end, return
123456
Else
123456abcdedf

As follows
5. The string to be taken to the specified character set. As in the following example, take a string that encounters an uppercase letter.
SSCANF ("123456abcdedfBCDEF", "%[^a-z]", buf);
printf ("%s\n", buf);
The result is: 123456ABCDEDF


6, given a string iios/[email protected], gets/And the string between @, first filter out "iios/", and then send a string of non-' @ ' to BUF
SSCANF ("Iios/[email protected", "%*[^/]/%[^@]", buf);
printf ("%s\n", buf);
The result is: 12DDWDFF

Here can be used to verify whether the input is a standard mailbox format,

#include <stdio.h>
int main ()
{
Char a[100],b[100],c[100],d[100];
int I, J, l;
scanf ("%s", a);
SSCANF (A, "%[^@]@%[^."). %s ", b,c,d);//This is not the @.
printf ("%s @%s". %s\n ", b,c,d);
The}//can be individually separated and then treated separately. So not much to say


7, given a string "" Hello, World ", only keep the world. (Note: "," followed by a space)
SSCANF ("Hello, World", "%*s%s", buf);
printf ("%s\n", buf);
The result: World
%*s indicates that the first match to the%s is filtered out, that is, Hello is filtered
If there are no spaces, the result is null.
The SSCANF function is very similar to regular expressions, but there is no strong regular expression, so it is recommended to use regular expressions for more complex string handling.
//-------------------------------------------------------
SSCANF, which represents the formatting of input from a string
The above means that from STR, the input number to X is 32700.
Long ago, I thought C had no own split string function, and then I found sscanf; all along, I thought sscanf could only delimit strings with spaces, and now I find I'm wrong.
SSCANF is a run-time function, and the prototype is simple:
int sscanf (
const Char *buffer,
const char *format [,
Argument] ...
);
Its powerful capabilities are reflected in the support for format.
I used to 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 then, I need to deal with 2006:03:18-2006:04:18.
Just canceled the '-' on both sides of the space, but broke the%s of the definition of the string.
Do I need to redesign a function to handle such a situation? It's not complicated, but, in order to have a uniform style for all of the code, I need to change a lot of places to replace the existing sscanf with my own split function. I thought I must have done it, and slept with a strong dissatisfaction with sscanf, waking up and finding it unnecessary.
A type field such as%[] in Format-type. If you read a string that is not delimited by a space, you can use%[].
%[] is similar to a regular expression. [A-z] means reading all characters 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);

Finally, there is an original test. If I want to judge that the first is not the element I need, then you can use the action of | or |, then how to write the code.

#include <stdio.h>
int main ()
{
Char buf[10]={0},buf1[10]={0};
SSCANF ("Hello,world", "%[{h}]%[{e}]s", BUF,BUF1);
printf ("%s\n%s", BUF,BUF1);
}

It is important to note that there is no answer if the E in your%[{e}] is written in L, because there is no corresponding code when reading to E, so it is skipped directly. Or, stop reading.

---restore content ends---

C Language Function Library learning ~sscanf~ formatting input

Related Article

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.