C++获取天气预报的问题

laobingyouzi 2008-10-07 08:20:36
这是我在网上下载的参考代码,如下:运行结果却不正确,不知哪里有问题,请高手指点一二
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#pragma comment(lib,"ws2_32.lib")
struct Weather_Info
{
char city[50];
char weather[50];
char temp[50];
char wind[50];
};
const char* URL="http://weather.tq121.com.cn/mapanel/index1.php?city=%s";
const char* weather_tag="<td width=\"160\" align=\"center\" valign=\"top\" class=\"weather\">";
const char* temp_tag="<td width=\"160\" align=\"center\" valign=\"top\" class=\"weatheren\">";
const char* wind_tag="<td width=\"153\" valign=\"top\"><span class=\"big-cn\">";
bool GetWeather(Weather_Info&wi,char *html)
{
if(html==NULL)
return false;
char *temp=NULL;
if((temp=strstr(html,weather_tag))==NULL)
return false;

temp+=strlen(weather_tag);
memcpy(wi.weather,temp,strlen(temp)-strlen(strstr(temp,"<")));
if((temp=strstr(html,temp_tag))==NULL)
return false;
temp+=strlen(temp_tag);
memcpy(wi.temp,temp,strlen(temp)-strlen(strstr(temp,"<")));
if((temp=strstr(html,wind_tag))==NULL)
return false;
temp+=strlen(wind_tag);
memcpy(wi.wind,temp,strlen(temp)-strlen(strstr(temp,"<")));
return true;
}
bool initsocket()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return false;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return false;
}
return true;

}
struct SearchInfo//ËÑË÷½á¹¹Ì嶨Òå
{
char host[256];//Ö÷»úÃû
unsigned int port;//¶Ë¿ÚºÅ
char filename[256];//ÒªÎļþÃû
char outfile[50];//±£´æÎļþÃû
};
void GetUrls(char *html);//½âÎöhtml´úÂëÖеÄURL
bool initsocket();//³õʼ»¯Ì×½Ó×Ö
bool initargs(SearchInfo &outinfo,int argc,char **args);//½âÎöÊäÈëÃüÁîÐвÎÊý
const char *HTTP_STR="http://";
const char* HTTP_REQUEST_HEADER= //HTTPÇëÇóÍ·
"GET %s HTTP/1.1\r\nAccept:*/*\r\n\
Accept-Language:zh-cn\r\n\
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n\
Host:%s\r\n\r\n";

bool initargs(SearchInfo &outinfo,const char*city)
{
memset(&outinfo,0,sizeof(SearchInfo));//³õʼ»¯

char *temp=new char[256];
memset(temp,0,256);
sprintf(temp,URL,city);
printf(temp);
temp=(strstr(temp,HTTP_STR)!=NULL)?temp+strlen(HTTP_STR):temp;//È¥µôÇ°ÃæµÄhttp£º\\
// printf("Url:%s\n",temp);
strcpy(outinfo.filename,strstr(temp,"/")!=NULL?strstr(temp,"/"):"/");//·ÖÎö³öÒªÏÂÔØÎļþÃû
int length=strstr(temp,"/")==NULL?strlen(temp):(strlen(temp)-strlen(strstr(temp,"/")));
//·ÖÎö³öÖ÷»úÃûµÄ³¤¶Èwww.zzti.edu.cn/index.html-/index.html
memcpy(outinfo.host,temp,length);//½âÎö³öÖ÷»úÃû
if((temp=strstr(outinfo.host,":"))!=NULL)//½âÎö¶Ë¿Ú
{
temp++;
outinfo.port=atoi(temp);
}
else//Èç¹ûûÓÐÊäÈëʹÓÃĬÈÏ80
{
outinfo.port=80;
}
delete temp;
return true;

}

char* GetFile(const SearchInfo * psi)
{
if(psi==NULL)
{
return NULL;
}
unsigned long serverip=0;//·þÎñÆ÷IP
if((serverip=inet_addr(psi->host))==INADDR_NONE)//Èç¹ûÖ÷»úÃû²»ÊÇIP
{
hostent *phst=gethostbyname(psi->host);//ÓÃDNS½âÎöÖ÷»úIP
if(phst==NULL)//Èç¹û½âÎöʧ°Ü·µ»Øfalse
return NULL;
//IN_ADDR in;
if(phst->h_addr_list[0]!=0)//½âÎö³É¹¦Ê¹ÓÃÖ÷»úµÚÒ»¸öIP
{
memcpy(&serverip,phst->h_addr_list[0],phst->h_length);
// in.S_un.S_addr=serverip;
}
// printf("IP:%s",inet_ntoa(in));
}
SOCKET s=socket(AF_INET,SOCK_STREAM,0);//´´½¨socket£¨TCPÁ¬½Ó£©
if(s==INVALID_SOCKET)//´´½¨Ê§°Ü
{
printf("Create socket Error!Error Code:%d\n",WSAGetLastError());
return NULL;
}
SOCKADDR_IN server_addr;//·þÎñÆ÷address
server_addr.sin_addr.S_un.S_addr=serverip;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(psi->port);
memset(server_addr.sin_zero,0,sizeof(server_addr.sin_zero));
printf("Begin Connect Server :%s On:%d\n",inet_ntoa(server_addr.sin_addr),psi->port);
//¿ªÊ¼Á¬½Ó·þÎñÆ÷·¢³öÇëÇó
if(SOCKET_ERROR==connect(s,(const sockaddr*)&server_addr,sizeof(SOCKADDR_IN)))
{
printf("Connect Server Error!Error Code:%d\n",WSAGetLastError());
closesocket(s);
//Èç¹ûÁ¬½Óʧ°Ü
return NULL;
}
printf("Connect Server OK!\n");
char buffer_sendmsg[256]={0};
sprintf(buffer_sendmsg,HTTP_REQUEST_HEADER,psi->filename,psi->host);//¹¹ÔìHTTPÇëÇó
//printf(buffer_sendmsg);
if(send(s,buffer_sendmsg,256,0)==SOCKET_ERROR)//Ïò·þÎñÆ÷·¢ËÍÇëÇó
{
printf("Send Request To Server Error!Error Code:%d\n",WSAGetLastError());
closesocket(s);
//·¢ËÍʧ°Ü
return NULL;
}
//´ò¿ªÎļþ¿ªÊ¼±£´æhtml´úÂë

int len=0;
char buffer_recv[1024]={0};//½ÓÊÕhtmlµÄbuffer

int sumlen=0;//htmlµÄ³¤¶È
char *html=(char*)malloc(sizeof(char)*1);//×ܵÄhtml×Ö·û´®

while((len=recv(s,buffer_recv,1024,0))!=0)
{
if(len==SOCKET_ERROR)
{
printf("Error in Recv Data!Error Code:%d\n",WSAGetLastError());
closesocket(s);

return NULL;
}
sumlen=strlen(html);
printf(buffer_recv);
//ÖØзÖÅäÄÚ´æÔ­À´´óС¼Ólen³¤¶È
if((html=(char*)realloc((void*)html,sumlen+sizeof(char)*len))!=NULL)
{
memset(html+sumlen-1,0,len);//½«Ð·ֵ½ÄÚ´æ³õʼΪ0
strcat(html,buffer_recv);//½«ÊÕµ½ÐÅϢдÈëзֵ½ÄÚ´æ
}
memset(buffer_recv,0,1024);
}

closesocket(s);
closesocket(s);

return strlen(html)==0?NULL:html;
}
void PrintWeather(const Weather_Info* pwi)
{
system("cls");
printf("\n");
printf("\n");
printf("\n");
printf(" \t ÌìÆøÔ¤±¨\n");
printf(" \t=====================================\n");
printf(" \t³Ç ÊУº%s\n",pwi->city);
printf(" \t------------------------------------\n");
printf(" \tÌì Æø£º%s\n",pwi->weather);
printf(" \t------------------------------------\n");
printf(" \tΠ¶È£º%s\n",pwi->temp);
printf(" \t------------------------------------\n");
printf(" \t·ç Ïò£º%s\n",pwi->wind);
printf(" \t======================================\n");
}
int main(int argc,char**argv)
{
Weather_Info wi;
SearchInfo si;
memset(&wi,0,sizeof(Weather_Info));
strcpy(wi.city,"Ö£ÖÝ");
if(argc>=2)
strcpy(wi.city,argv[1]);
if(!initsocket())
{
printf("Socket Error~\n");
return 1;
}

memset(&si,0,sizeof(SearchInfo));
sprintf(si.host,URL,wi.city);
if(!initargs(si,wi.city))
{
printf("Connect Internet Error~\n");
return 1;
}
char *html=NULL;
if((html=GetFile(&si))==NULL)
{
printf("Get Information Error~!\n");
return 1;
}

if(!GetWeather(wi,html))
{
printf("Get Weather Info Error~\n");
return 1;
}
free(html);
PrintWeather(&wi);
system("pause");
getchar();
return 0;
}
...全文
653 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
laobingyouzi 2008-10-15
  • 打赏
  • 举报
回复
程序是没有问题的,程序直接拷出来的,我在别人电脑上运行过(有兴趣的朋友可以拷下来试下,自己再排下版,运行因该没问题的),但是在我自己电脑上是运行不了的,我但不跟踪发现“hostent *phst=gethostbyname(psi->host);//用DNS解析主机IP”运行到此处时,phst为NULL,也时就说DNS没解析除主机IP,后来我发现我电脑IP设置的地方没有设置DNS(我是用代理上网的),现在的问题怎么将这个问题解决,也就说怎么实现只要上网就可以获取的天气预报,我曾经下载过一个日历(有天气预报功能),在我电脑上运行后,该日历可以获取天气预报的。我要实现类似的功能。
aaajj 2008-10-11
  • 打赏
  • 举报
回复
识之
mabo321 2008-10-11
  • 打赏
  • 举报
回复
好贴,,

顶一个~~~~
baihacker 2008-10-11
  • 打赏
  • 举报
回复
我有程序...MFC + boost的...
laobingyouzi 2008-10-11
  • 打赏
  • 举报
回复
我后来又试了一下,程序在别的电脑是可以用的,但是我发现在我电脑用确用不了,跟踪程序,发现在域名解析的时候出了问题,我的网络设置没有设置DNS服务器。不知有什么方法可解决此问题,---只要能上网就可以获得天气预报?
malu_1982 2008-10-11
  • 打赏
  • 举报
回复
天气预报
貌似应该用Observer模式。逻辑比较清晰,低耦合。
冰山之海 2008-10-11
  • 打赏
  • 举报
回复
我改了一下,他解析天气有误
// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <Winsock2.h>
#include <iostream.h>

#pragma comment(lib,"ws2_32.lib")

struct Weather_Info
{
char city[50];
char weather[50];
char temp[50];
char wind[50];
};
const char* URL="http://weather.tq121.com.cn/mapanel/index1.php?city=%s";
const char* weather_tag="class=\"weather\">";
const char* temp_tag="class=\"weatheren\">";
const char* wind_tag="class=\"big-cn\">";
bool GetWeather(Weather_Info&wi,char *html)
{
if(html==NULL)
return false;
char *temp=NULL;
if((temp=strstr(html,weather_tag))==NULL)
return false;

temp+=strlen(weather_tag);
memcpy(wi.weather,temp,strstr(temp,"<")-temp);

if((temp=strstr(html,temp_tag))==NULL)
return false;
temp+=strlen(temp_tag);
// memcpy(wi.temp,temp,strlen(temp)-strlen(strstr(temp," <")));
memcpy(wi.temp,temp,strstr(temp,"<")-temp);
if((temp=strstr(temp,wind_tag))==NULL)
return false;
temp+=strlen(wind_tag);
memcpy(wi.wind,temp,strstr(temp,"<")-temp);
//memcpy(wi.wind,temp,strlen(temp)-strlen(strstr(temp," <")));
return true;
}
bool initsocket()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return false;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return false;
}
return true;

}
struct SearchInfo
{
char host[256];
unsigned int port;
char filename[256];
char outfile[50];
};
void GetUrls(char *html);
bool initsocket();
bool initargs(SearchInfo &outinfo,int argc,char **args);
const char *HTTP_STR="http://";
const char* HTTP_REQUEST_HEADER=
"GET %s HTTP/1.1\r\nAccept:*/*\r\n\ Accept-Language:zh-cn\r\n\User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n\Host:%s\r\n\r\n";

bool initargs(SearchInfo &outinfo,const char*city)
{
memset(&outinfo,0,sizeof(SearchInfo));

char *temp=new char[256];
memset(temp,0,256);
sprintf(temp,URL,city);
printf(temp);
temp=(strstr(temp,HTTP_STR)!=NULL)?temp+strlen(HTTP_STR):temp;
strcpy(outinfo.filename,strstr(temp,"/")!=NULL?strstr(temp,"/"):"/");
int length=strstr(temp,"/")==NULL?strlen(temp):(strlen(temp)-strlen(strstr(temp,"/")));

memcpy(outinfo.host,temp,length);
if((temp=strstr(outinfo.host,":"))!=NULL)
{
temp++;
outinfo.port=atoi(temp);
}
else
{
outinfo.port=80;
}
delete temp;
return true;

}

char* GetFile(const SearchInfo * psi)
{
if(psi==NULL)
{
return NULL;
}
unsigned long serverip=0;
if((serverip=inet_addr(psi->host))==INADDR_NONE)
{
hostent *phst=gethostbyname(psi->host);
if(phst==NULL)
return NULL;

if(phst->h_addr_list[0]!=0)
{
memcpy(&serverip,phst->h_addr_list[0],phst->h_length);

}

}
SOCKET s=socket(AF_INET,SOCK_STREAM,0);
if(s==INVALID_SOCKET)
{
printf("Create socket Error!Error Code:%d\n",WSAGetLastError());
return NULL;
}
SOCKADDR_IN server_addr;
server_addr.sin_addr.S_un.S_addr=serverip;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(psi->port);
memset(server_addr.sin_zero,0,sizeof(server_addr.sin_zero));

if(SOCKET_ERROR==connect(s,(const sockaddr*)&server_addr,sizeof(SOCKADDR_IN)))
{
printf("Connect Server Error!Error Code:%d\n",WSAGetLastError());
closesocket(s);

return NULL;
}
char buffer_sendmsg[256]={0};
sprintf(buffer_sendmsg,HTTP_REQUEST_HEADER,psi->filename,psi->host);
// printf("\n请求格式:%s\n",buffer_sendmsg);
if(send(s,buffer_sendmsg,256,0)==SOCKET_ERROR)
{
printf("\n城市输入有误,任意键退出\n");
closesocket(s);
system("pause");
getchar();
return NULL;
}


int len=0;
char buffer_recv[1024]={0};

int sumlen=0;
char *html=(char*)malloc(sizeof(char)*1);

while((len=recv(s,buffer_recv,1024,0))!=0)
{
if(len==SOCKET_ERROR)
{
printf("网络无回复,任意键退出\n");
closesocket(s);
system("pause");
getchar();
return NULL;
}
sumlen=strlen(html);

// printf(buffer_recv);

if((html=(char*)realloc((void*)html,sumlen+sizeof(char)*len))!=NULL)
{
memset(html+sumlen-1,0,len);
strcat(html,buffer_recv);
}
memset(buffer_recv,0,1024);
}

closesocket(s);
closesocket(s);

return strlen(html)==0?NULL:html;
}
void PrintWeather(const Weather_Info* pwi)
{
//system("cls");
printf("\n");
printf("\n");
printf("\n");
printf(" \t 天气预报 \n");
printf(" \t=====================================\n");
printf(" \t城市 : %s\n",pwi->city);
printf(" \t------------------------------------\n");
printf(" \t天气情况: %s\n",pwi->weather);
printf(" \t------------------------------------\n");
printf(" \t气温 : %s\n",pwi->temp);
printf(" \t------------------------------------\n");
printf(" \t风力 : %s\n",pwi->wind);
printf(" \t======================================\n");
printf(" \t版权所有:一生有你 \n");
}
int main(int argc,char**argv)
{
Weather_Info wi;
SearchInfo si;
char szCity[20] = "";
memset(&wi,0,sizeof(Weather_Info));

cout<<"输入你要查询城市的名称:";
cin>>szCity;

strcpy(wi.city,szCity);
if(argc>=2)
strcpy(wi.city,argv[1]);
if(!initsocket())
{
printf("Socket Error1~\n");
return 1;
}

memset(&si,0,sizeof(SearchInfo));
sprintf(si.host,URL,wi.city);
if(!initargs(si,wi.city))
{
printf("Connect Internet Error2~\n");
return 1;
}
char *html=NULL;
if((html=GetFile(&si))==NULL)
{
printf("Get Information Error3~!\n");
return 1;
}

if(!GetWeather(wi,html))
{
printf("\n\n\t\t警告: 城市输入有误\n\n\n");
system("pause");
getchar();
return 1;
}
free(html);
PrintWeather(&wi);
printf("\n\n");
system("pause");
getchar();
return 0;
}

冰山之海 2008-10-11
  • 打赏
  • 举报
回复
经典,这跟在网页上搜索是一个道理,如果你能写个解析HTML语言就更好了,就能把天气预报在dos下打印出来
frank_ll 2008-10-11
  • 打赏
  • 举报
回复
mark
Smile_unix 2008-10-08
  • 打赏
  • 举报
回复
^_^
qkhhxkj102 2008-10-07
  • 打赏
  • 举报
回复
我的天呀
chlaws 2008-10-07
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
struct Weather_Info
{
char city[50];
char weather[50];
char temp[50];
char wind[50];
};
const char* URL="http://weather.tq121.com.cn/mapanel/index1.php?city=%s";
const char* weather_tag=" <td width=\"160\" align=\"center\" valign=\"top\" class=\"weather\">";
const char* temp_tag=" <td width=\"160\" align=\"center\" valign=\"top\" class=\"weatheren\">";
const char* wind_tag=" <td width=\"153\" valign=\"top\"> <span class=\"big-cn\">";
bool GetWeather(Weather_Info&wi,char *html)
{
if(html==NULL)
return false;
char *temp=NULL;
if((temp=strstr(html,weather_tag))==NULL)
return false;

temp+=strlen(weather_tag);
memcpy(wi.weather,temp,strlen(temp)-strlen(strstr(temp," <")));
if((temp=strstr(html,temp_tag))==NULL)
return false;
temp+=strlen(temp_tag);
memcpy(wi.temp,temp,strlen(temp)-strlen(strstr(temp," <")));
if((temp=strstr(html,wind_tag))==NULL)
return false;
temp+=strlen(wind_tag);
memcpy(wi.wind,temp,strlen(temp)-strlen(strstr(temp," <")));
return true;
}
bool initsocket()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return false;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return false;
}
return true;

}
struct SearchInfo//???÷?á11ì??¨ò?
{
char host[256];//?÷?ú??
unsigned int port;//???úo?
char filename[256];//òa???t??
char outfile[50];//±£′????t??
};
void GetUrls(char *html);//?a??html′ú???Dμ?URL
bool initsocket();//3?ê??ˉì×?ó×?
bool initargs(SearchInfo &outinfo,int argc,char **args);//?a??ê?è??üá?DD2?êy
const char *HTTP_STR="http://";
const char* HTTP_REQUEST_HEADER=/*HTTP???óí·*//*这串常量字符串不能多行,只能放一行*/
"GET %s HTTP/1.1\r\nAccept:*/*\r\n\ Accept-Language:zh-cn\r\n\User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n\Host:%s\r\n\r\n";

bool initargs(SearchInfo &outinfo,const char*city)
{
memset(&outinfo,0,sizeof(SearchInfo));//3?ê??ˉ

char *temp=new char[256];
memset(temp,0,256);
sprintf(temp,URL,city);
printf(temp);
temp=(strstr(temp,HTTP_STR)!=NULL)?temp+strlen(HTTP_STR):temp;//è¥μ??°??μ?http£o\\
// printf("Url:%s\n",temp);
strcpy(outinfo.filename,strstr(temp,"/")!=NULL?strstr(temp,"/"):"/");//·???3?òa???????t??
int length=strstr(temp,"/")==NULL?strlen(temp):(strlen(temp)-strlen(strstr(temp,"/")));
//·???3??÷?ú??μ?3¤?èwww.zzti.edu.cn/index.html-/index.html
memcpy(outinfo.host,temp,length);//?a??3??÷?ú??
if((temp=strstr(outinfo.host,":"))!=NULL)//?a?????ú
{
temp++;
outinfo.port=atoi(temp);
}
else//è?1???óDê?è?ê1ó???è?80
{
outinfo.port=80;
}
delete temp;
return true;

}

char* GetFile(const SearchInfo * psi)
{
if(psi==NULL)
{
return NULL;
}
unsigned long serverip=0;//·t???÷IP
if((serverip=inet_addr(psi->host))==INADDR_NONE)//è?1??÷?ú??2?ê?IP
{
hostent *phst=gethostbyname(psi->host);//ó?DNS?a???÷?úIP
if(phst==NULL)//è?1??a??꧰ü·μ??false
return NULL;
//IN_ADDR in;
if(phst->h_addr_list[0]!=0)//?a??3é1|ê1ó??÷?úμúò???IP
{
memcpy(&serverip,phst->h_addr_list[0],phst->h_length);
// in.S_un.S_addr=serverip;
}
// printf("IP:%s",inet_ntoa(in));
}
SOCKET s=socket(AF_INET,SOCK_STREAM,0);//′′?¨socket£¨TCPá??ó£?
if(s==INVALID_SOCKET)//′′?¨ê§°ü
{
printf("Create socket Error!Error Code:%d\n",WSAGetLastError());
return NULL;
}
SOCKADDR_IN server_addr;//·t???÷address
server_addr.sin_addr.S_un.S_addr=serverip;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(psi->port);
memset(server_addr.sin_zero,0,sizeof(server_addr.sin_zero));
printf("Begin Connect Server :%s On:%d\n",inet_ntoa(server_addr.sin_addr),psi->port);
//?aê?á??ó·t???÷·¢3????ó
if(SOCKET_ERROR==connect(s,(const sockaddr*)&server_addr,sizeof(SOCKADDR_IN)))
{
printf("Connect Server Error!Error Code:%d\n",WSAGetLastError());
closesocket(s);
//è?1?á??ó꧰ü
return NULL;
}
printf("Connect Server OK!\n");
char buffer_sendmsg[256]={0};
sprintf(buffer_sendmsg,HTTP_REQUEST_HEADER,psi->filename,psi->host);//11?ìHTTP???ó
//printf(buffer_sendmsg);
if(send(s,buffer_sendmsg,256,0)==SOCKET_ERROR)//?ò·t???÷·¢?í???ó
{
printf("Send Request To Server Error!Error Code:%d\n",WSAGetLastError());
closesocket(s);
//·¢?í꧰ü
return NULL;
}
//′ò?a???t?aê?±£′?html′ú??

int len=0;
char buffer_recv[1024]={0};//?óê?htmlμ?buffer

int sumlen=0;//htmlμ?3¤?è
char *html=(char*)malloc(sizeof(char)*1);//×üμ?html×?·?′?

while((len=recv(s,buffer_recv,1024,0))!=0)
{
if(len==SOCKET_ERROR)
{
printf("Error in Recv Data!Error Code:%d\n",WSAGetLastError());
closesocket(s);

return NULL;
}
sumlen=strlen(html);
printf(buffer_recv);
//??D?·????ú′??-à′′óD??ólen3¤?è
if((html=(char*)realloc((void*)html,sumlen+sizeof(char)*len))!=NULL)
{
memset(html+sumlen-1,0,len);//??D?·?μ??ú′?3?ê??a0
strcat(html,buffer_recv);//??ê?μ?D??¢D′è?D?·?μ??ú′?
}
memset(buffer_recv,0,1024);
}

closesocket(s);
closesocket(s);

return strlen(html)==0?NULL:html;
}
void PrintWeather(const Weather_Info* pwi)
{
system("cls");
printf("\n");
printf("\n");
printf("\n");
printf(" \t ìì???¤±¨\n");
printf(" \t=====================================\n");
printf(" \t3? êD£o%s\n",pwi->city);
printf(" \t------------------------------------\n");
printf(" \tìì ??£o%s\n",pwi->weather);
printf(" \t------------------------------------\n");
printf(" \t?? ?è£o%s\n",pwi->temp);
printf(" \t------------------------------------\n");
printf(" \t·? ?ò£o%s\n",pwi->wind);
printf(" \t======================================\n");
}
int main(int argc,char**argv)
{
Weather_Info wi;
SearchInfo si;
memset(&wi,0,sizeof(Weather_Info));
strcpy(wi.city,"?£?Y");
if(argc>=2)
strcpy(wi.city,argv[1]);
if(!initsocket())
{
printf("Socket Error~\n");
return 1;
}

memset(&si,0,sizeof(SearchInfo));
sprintf(si.host,URL,wi.city);
if(!initargs(si,wi.city))
{
printf("Connect Internet Error~\n");
return 1;
}
char *html=NULL;
if((html=GetFile(&si))==NULL)
{
printf("Get Information Error~!\n");
return 1;
}

if(!GetWeather(wi,html))
{
printf("Get Weather Info Error~\n");
return 1;
}
free(html);
PrintWeather(&wi);
system("pause");
getchar();
return 0;
}


69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧