|
|
小弟遇到一个很奇怪的输入格式问题,麻烦大家解答一下。代码如下:
- [color=Red]#define OCTETRANGE "%3d%*1[-]%3d"
- #define OCTET "%3d"
- #define DOT "%*1[.]"
- #define COMP "%7[0-9-]"
- #define REMINDER "%s"[/color]
- static int
- range(data, s, e)
- char * data;
- int * s;
- int * e;
- {
- int convs;
- int first, last;
-
- [color=Red]convs=sscanf(data, OCTETRANGE, &first, &last);[/color]
- if (convs != 2)
- {
- /* it didn't work out, so we try converting it as
- an OCTET (xxx) */
- [color=Red] convs=sscanf(data, OCTET, &first);[/color]
- if (convs != 1)
- {
- /* that didn't work out either, so it's not a range */
- return (-1);
- }
- else
- {
- /* we'll use these as loop ranges later */
- last = first;
- }
- }
-
- if((first < 0) || (first > 255) ||
- (last < 0 ) || (last > 255))
- return (-1);
-
-
- if(first > last)
- {
- /* swap the two vars */
- [color=Red] first ^= last;
- last ^= first;
- first ^= last;[/color]
- }
-
- if(s)*s = first;
- if(e)*e = last;
- return 0;
复制代码
此段程序应该是判断ip方面的功能。红色字体都是小弟不明白的地方。麻烦大家指点一下。下面的程序是我写的测试程序.不知道为什么每次sscanf s[] 后,s就打印不出来。
- #include<stdio.h>
- #define OCTETRANGE "%3d%*1[-]%3d"
- #define OCTET "%3d"
- #define DOT "%*1[.]"
- #define COMP "%7[0-9-]"
- #define REMINDER "%s"
- main()
- {
- char s[20]="2399defg[-] 2456";
- int a=1,b=1,c=3;
- c=sscanf(s, OCTETRANGE, &a, &b);
- printf("s=%s\n c=%4d a=%4d b=%4d\n",s,c,a,b);
- //s[20]="abcdefg";
- c=sscanf(s, COMP, &a, &b);
- printf("s=%s\n c=%4d a=%4d b=%4d\n",s,c,a,b);
- c=sscanf(s, OCTET, &a, &b);
- printf("s=%s\n c=%4d a=%4d b=%4d\n",s,c,a,b);
- printf(OCTETRANGE,5,6789,7);
- printf("\n****************************\n");
- printf(COMP,4444);
- printf("\n****************************\n");
- printf(OCTET,4);
- printf("\n****************************\n");
- printf(DOT,456);
- printf("\n");
- }
复制代码 |
|