天之骄子 发表于 2013-8-4 15:03:02

sscanf()函数题

#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){   char buf,str1,str2,str3,str4,temp="<default>";
   int count;   scanf("%d",&count);   while(count--)   {      str1='\0';      str2='\0';      str3='\0';      str4='\0';      scanf("%s",buf);      sscanf(buf,"%[^:]://%[^:,/]:%[,1-9]",str1,str2,str3,str4);    //??      sscanf(buf,"%[^:]://%[^:,/]/%",str1,str2,str4);   //??      if(str3=='\0')strcpy(str3,temp);          if(str4=='\0')strcpy(str4,temp);      printf("%s\n%s\n%s\n%s\n",str1,str2,str3,str4);   }   return 0;}

565123 发表于 2013-8-5 00:04:08

读取字符串
解析网址
str1为协议,例如ftp,http
str2为网站地址
str3为端口号
str4为网站路径
改良后的代码
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv [])
{
        char buf, str1, str2, str3, str4, temp = "<default>";

        int count;
        scanf("%d", &count);
        while (count--)
        {
                str1 = '\0';
                str2 = '\0';
                str3 = '\0';
                str4 = '\0';
                scanf("%s", buf);
                sscanf(buf, "%[^:]://%[^:/]:%", str1, str2, str3);    //??
                sscanf(buf, "%[^:]://%[^:/]%*[^/]/%", str1, str2, str4);   //??
                if (str3 == '\0')strcpy(str3, temp);
                if (str4 == '\0')sscanf(buf, "%[^:]://%[^:/]/%", str1, str2, str4);
                if (str4 == '\0')strcpy(str4, temp);
                printf("%s\n%s\n%s\n%s\n", str1, str2, str3, str4);
        }
        return 0;
}

565123 发表于 2013-8-5 00:39:02

本帖最后由 565123 于 2013-8-5 00:40 编辑

http://bbs.fishc.com:80/thread-35348-1-1.html

输出
http
bbs.fishc.com
80
thread-35348-1-1.html

Skyline 发表于 2013-8-6 08:04:16

好像看不懂啊?。。。。。。。。

565123 发表于 2013-8-6 19:17:28

Skyline 发表于 2013-8-6 08:04 static/image/common/back.gif
好像看不懂啊?。。。。。。。。

请百度搜Regular Expression

zZ_小春 发表于 2013-8-21 10:26:49

:shy:居然还有sscanf函数,百度一下涨知识去,哈哈
页: [1]
查看完整版本: sscanf()函数题