亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 951 | 回復(fù): 0
打印 上一主題 下一主題

Linux C寫(xiě)的終端天氣預(yù)報(bào)查詢 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-01-26 15:44 |只看該作者 |倒序?yàn)g覽

weather.c文件:
###########################
#include <stdio.h>



#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "get_detail_from_html.h"

#define LINE_LEN    1024
#define DAY_TAG_STRING        "今天白天"
#define NIGHT_TAG_STRING    "今天夜間"
#define PHP_NAME            "/tmp/weather.php"
#define HTML_NAME            "/tmp/weather.html"

typedef struct _info
{
    unsigned char DON;//白天還是夜間,0:day,1:night
    char t[16];//實(shí)際溫度
    char f[16];//體感溫度
    char type[16];//天氣類(lèi)型
    char wing[32];//風(fēng)向
}info_t;

typedef struct _weather
{
    info_t day;
    info_t night;
    unsigned char gd;
    unsigned char gnt;
}weather_t,*weather_p;

//int readfile(const char* filename);
int get_weather_info(const char* filename,weather_p info);

int main(int argc, char **argv)
{
    if(argc != 2)
    {
        printf("Usage: %s <city-name>\n",argv[0]);
    }
    char* cityname=argv[1];
    char cmd[128];
    pid_t pid;
    weather_t weather;
    memset(&weather,0,sizeof(weather_t));
    weather.day.DON=0;
    weather.night.DON=1;
  
    printf("Connecting ...\n");
    //get the weather file from sina weather web by
    //wget -O weather.php http://php.weather.sina.com.cn/search.php?city=CITYNAME
    //and turn gbk to utf8 by iconv -f gbk -t utf8 xxx.php > xxx.html
    sprintf(cmd,"wget -q -O %s http://php.weather.sina.com.cn/search.php?city=%s",PHP_NAME,cityname);
    system(cmd);
    sprintf(cmd,"iconv -f gbk -t utf8 %s > %s",PHP_NAME,HTML_NAME);
    system(cmd);
  
    //switch(readfile(filename))
    switch(get_weather_info(HTML_NAME,&weather))
    {
        case 0:
            printf("%s's weather is:\n",cityname);
            if(weather.gd)
            {
                printf("day:\t%s\t%s\t%s\n",weather.day.t,weather.day.type,weather.day.wing);
            }
            if(weather.gnt)
            {
                printf("night:\t%s\t%s\t%s\n",weather.night.t,weather.night.type,weather.night.wing);
            }
            break;
        default:
            printf("error.please try again and make sure the city name be true.\n");
            break;
    }
    return 0;
}

int get_weather_info(const char* filename,weather_p weather)
{
    FILE *fp;
    char rline[LINE_LEN];
    int flag=0;
    char* rs;
  
    if((fp = fopen(filename,"r"))==NULL)
    {
        perror("fopen");
        return -1;
    }
  
    while(1)
    {
        rs=fgets(rline,LINE_LEN,fp);
        if(flag==2 || rs==NULL || !strlen(rline)) break;
        if(strstr(rline,DAY_TAG_STRING))
        {
            flag=1;
            //printf("%s\n",rline);
            //get day weather information.
            err_info err;
            weather->gd=1;
            while(!strstr(fgets(rline,LINE_LEN,fp),"<li>"));
            get_detail_from_html(rline,weather->day.t,sizeof(weather->day.t),"span",&err);
            if(err!=ERR_NO)
            {
                weather->gd=0;
                printf("day t error: %s\n",get_err_info(err));
            }
          
            fgets(rline,LINE_LEN,fp);
            get_detail_from_html(rline,weather->day.type,sizeof(weather->day.type),"li",&err);
            if(err!=ERR_NO)
            {
                weather->gd=0;
                printf("day type error: %s\n",get_err_info(err));
            }
          
            fgets(rline,LINE_LEN,fp);
            get_detail_from_html(rline,weather->day.wing,sizeof(weather->day.wing),"li",&err);
            if(err!=ERR_NO)
            {
                weather->gd=0;
                printf("day wing error: %s\n",get_err_info(err));
            }
        }
        if(strstr(rline,NIGHT_TAG_STRING))
        {
            flag=2;
            //printf("%s\n",rline);
            //get day weather information.
            weather->gnt=1;
            err_info err;
            while(!strstr(fgets(rline,LINE_LEN,fp),"<li>"));
            get_detail_from_html(rline,weather->night.t,sizeof(weather->night.t),"span",&err);
            if(err!=ERR_NO)
            {
                weather->gd=0;
                printf("night t error: %s\n",get_err_info(err));
            }
          
            fgets(rline,LINE_LEN,fp);
            get_detail_from_html(rline,weather->night.type,sizeof(weather->night.type),"li",&err);
            if(err!=ERR_NO)
            {
                weather->gd=0;
                printf("night type error: %s\n",get_err_info(err));
            }
          
            fgets(rline,LINE_LEN,fp);
            get_detail_from_html(rline,weather->night.wing,sizeof(weather->night.wing),"li",&err);
            if(err!=ERR_NO)
            {
                weather->gd=0;
                printf("night wing error: %s\n",get_err_info(err));
            }
        }
    }
    fclose(fp);
  
    return 0;

}

################################
get_detail_from_html.h


#ifndef GET_DETAIL_FROM_HTML_H
#define GET_DETAIL_FROM_HTML_H

typedef enum _err_info
{
    ERR_NO=0,
    ERR_NOT_FIND,
    ERR_DST_SHORT
}err_info;

char* get_detail_from_html(const char* source,char* dst,size_t s_t,const char* html_flag,err_info* err);
char* get_err_info(err_info err);

#endif //GET_DETAIL_FROM_HTML_H
###############################

get_detail_from_html.c

#include <string.h>
#include <stdio.h>
#include "get_detail_from_html.h"

char* get_detail_from_html(const char* source,char* dst,size_t s_t,const char* html_flag,err_info* err)//html_flag e.g: body



{
    char *s=NULL,*e=NULL,*d=NULL;
    char html_end[32];
    sprintf(html_end,"</%s>\0",html_flag);
    d=dst;
    //判斷是否存在標(biāo)簽
    s=(char*)source;
    e=s;
    while((s=strstr(e,html_flag)))
    {
        if(*(s-1)=='<') break;
        e=s+1;
    }
    if(!s)
    {
        *err=ERR_NOT_FIND;
        return s;
    }
    if(!(e=strstr(s,html_end)))
    {
        *err=ERR_NOT_FIND;
        return e;
    }
    while(*s!='>' && s<e) s++;
    if(!(s<e))
    {
        *err=ERR_NOT_FIND;
        return (char*)0;
    }
    //存在標(biāo)簽
    s++;
    if(s_t<(e-s-1))
    {
        printf("%d:%d\n",s_t,(e-s-1));
        *err=ERR_DST_SHORT;
        return (char*)0;
    }
    memcpy(dst,s,e-s);
    dst[e-s]='\0';
    *err=ERR_NO;
    return d;
}

char* get_err_info(err_info err)
{
    switch(err)
    {
        case ERR_DST_SHORT:
            return "dst lenght is too short.";
        break;
        case ERR_NOT_FIND:
            return "cannot find tag.";
        break;
        case ERR_NO:
            return "";
        break;
        default:
            return "Unknow error.";
        break;
    }
}


#############################
Makefile

CC=gcc
OBJ=weather.o get_detail_from_html.o
all:weather
weather:$(OBJ)
    $(CC) $^ -o $@
weather.o:weather.c get_detail_from_html.h
    $(CC) -c $< -o $@
get_detail_from_html.o: get_detail_from_html.c get_detail_from_html.h
    $(CC) -c $< -o $@
clean:
    -rm weather $(OBJ)

############################

您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP