设为首页 加入收藏

TOP

Linux C写的终端天气预报查询(一)
2014-11-24 07:32:34 来源: 作者: 【 】 浏览:3
Tags:Linux 终端 天气预报 查询

Linux C写的终端天气预报查询截图见下,不多说,分享一下代码:


weather.c文件:
###########################
#include
#include
#include
#include
#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];//实际温度
char f[16];//体感温度
char type[16];//天气类型
char wing[32];//风向
}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 \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),"

  • "));
    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,weat
  • 首页 上一页 1 2 下一页 尾页 1/2/2
    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    分享到: 
    上一篇C#连接远程Oracle连接字符串 下一篇基于Linux C 的简单聊天程序

    评论

    帐  号: 密码: (新用户注册)
    验 证 码:
    表  情:
    内  容:

    ·Java 并发工具类:提 (2025-12-25 20:25:44)
    ·Java面试技巧:如何 (2025-12-25 20:25:41)
    ·Java并发编程中的线 (2025-12-25 20:25:38)
    ·C 语言 - cppreferen (2025-12-25 19:50:27)
    ·《C 语言入门教程》 (2025-12-25 19:50:23)