First, the prototype of a c function is given: int sscanf (const char * buffer, const char * format, [argument]...) its return value is the parameter data, that is, the number of argument, buffer: the stored data, format: format control string, argument: select the string. This program reads data from the standard stream and can carry out unlimited input. The following code is pasted, which leads to another problem. Convert the string ip address to an integer ip address.
[Cpp]
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char str [32];
Int a, B, c, d;
Int ret = 0;
While (fgets (str, sizeof (str), stdin )! = NULL)
{
Int len = strlen (str );
Str [len] = '\ 0 ';
Ret = sscanf (str, "% d. % d", & a, & B, & c, & d );
If (ret = 4 & (a> = 0 & a <= 255) & (B> = 0 & B <= 255) & (c> = 0 & c <= 255) & (d> = 0 & d <= 255 ))
{
Printf ("it is ip! \ N ");
}
Else
Printf ("it is not ip! \ N ");
}
Return 0;
}
Gcc-Wall ip. c-o ip
12.3.4.5
It is a ip address!
The following introduces another problem. In many cases, the string ip address must be converted to an integer ip address. You can also apply the sscanf function to store the four fields to a, B, c and d are removed from the four variables before the shift operation. Because the IP address is a 32-bit and unsigned integer variable, the unsigned int can be used for storage. unsinged int ip = (a <24) + (B <16) + (c <8) + d.