VC获取天气

// weather.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"


//#include <iostream> 
//#include <winsock.h> 
//#include <string> 
//#pragma comment(lib, "ws2_32.lib") 
//using namespace std;
//int main(int argc, char* argv[])
//{
//struct hostent *pURL;
//    struct sockaddr_in addr; 
//    WSADATA WSAData={0}; 
//    SOCKET sockfd; 
//    string header;
//    static char text[BUFSIZ];
// 
//    WSAStartup(MAKEWORD(2,2), &WSAData);
//    sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 
//    pURL = gethostbyname("www.webxml.com.cn"); 
//    addr.sin_family = AF_INET; 
//    addr.sin_addr.s_addr = *((unsigned long*)pURL->h_addr); 
//    addr.sin_port = htons(80);
//    connect(sockfd,(SOCKADDR *)&addr,sizeof(addr));
//    header="GET ";
//    header.append("/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=57073");
//    header.append(" HTTP/1.1\r\n");
//    header.append("HOST: ");
//    header.append("www.webxml.com.cn");
//    header.append("\r\nConnection: Close\r\n\r\n");
// 
//    send(sockfd, header.c_str(), strlen(header.c_str()), 0);
// 
//    while (recv(sockfd, text, BUFSIZ, 0) != NULL) 
//    { 
//        cout<<text;
//        strnset(text, '\0', BUFSIZ); 
//    } 
//    closesocket(sockfd); 
//    WSACleanup(); 
//    system("PAUSE");
//}


#include <stdio.h>
#include <windows.h>
#include <wininet.h>
#include <locale.h>
#pragma comment(lib,"Wininet.lib")
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <qstring.h>
#include <stdlib.h>//char转int
#include <read.h>//char转int
#include <cassert>
#include <qstring.h>


using namespace std; 
int line_;
string result_;
CCom * cm;






//**写文件函数  
void writeFile(char * char_)
{




 int x=6;
 FILE *fp=fopen("text.txt","w");
 //fprintf(fp,"yes you are right !\ny=4+%d",x);
 fprintf(fp,char_);
 fclose(fp);  








}




bool readxml()
{
int ii=0;
    vector<char> v;
    CHAR szUrl[] = "http://flash.weather.com.cn/wmaps/xml/china.xml";


// unsigned short *P="http://flash.weather.com.cn/wmaps/xml/china.xml";
    CHAR szAgent[] = "";
    HINTERNET hInternet1 = 
        InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL);
    if (NULL == hInternet1)
     {
        InternetCloseHandle(hInternet1);
        return FALSE;
     }
    HINTERNET hInternet2 = 
        InternetOpenUrl(hInternet1,szUrl,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE,NULL);
///切换到“c/C++ ” Tab页 从下拉列表框中选择 “Preprocessor” ,去掉_MBCS,添加UNICODE,_UNICODE(注意逗号 把UNICODE去掉不报错了
    if (NULL == hInternet2)
     {
        InternetCloseHandle(hInternet2);
        InternetCloseHandle(hInternet1);
        return FALSE;
     }
    DWORD dwMaxDataLength = 500;
    PBYTE pBuf = (PBYTE)malloc(dwMaxDataLength*sizeof(TCHAR));
    if (NULL == pBuf)
     {
        InternetCloseHandle(hInternet2);
        InternetCloseHandle(hInternet1);
        return FALSE;
     }
    DWORD dwReadDataLength = NULL;
    BOOL bRet = TRUE;
    do 
    {
        ZeroMemory(pBuf,dwMaxDataLength*sizeof(TCHAR));
        bRet = InternetReadFile(hInternet2,pBuf,dwMaxDataLength,&dwReadDataLength);
        for (DWORD dw = 0;dw < dwReadDataLength;dw++)
         {
            v.push_back(pBuf[dw]);
         }
     } while (NULL != dwReadDataLength);
    vector<char>::iterator i;
setlocale(LC_CTYPE,"chs");
    wprintf(L"显示中文\n");
    for(i=v.begin(); i!=v.end(); i++)


{ //wprintf(L"%c",*i);
 printf("%c",*i);
   
   ii=ii++;
   Sleep(1);
}
Sleep(1000);
//下面是将vector<char> v 转换为char * 并写入txt
//vector是一个动态数组,如果定义为vector<char>,则是一个char数组
     char *str = new char[v.size()+1];
     copy(v.begin(),v.end(), str);
     str[v.size()]=0;
     cout<<str<<endl;
writeFile(str);
     delete []str;




}
//***** 逐行读入txt
void readTxt(string file)
{
//setlocale( LC_ALL, ".936" );
    ifstream infile; 
    infile.open(file.data());   //将文件流对象与文件连接起来 
    assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行 


    string s;
    while(getline(infile,s))
    {
       // cout<<s<<endl;
line_=line_+1;
if (line_==14)
result_=s;
    }
    infile.close();             //关闭文件输入流 
}
//逐个字符读入(包括空格与回车)
void readTxt1(string file)
{
    ifstream infile; 
    infile.open(file.data());   //将文件流对象与文件连接起来 
    assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行 


    char c;
    infile >> noskipws;
    while (!infile.eof())
    {
        infile>>c;
        cout<<c<<endl;


    }
    infile.close();             //关闭文件输入流 
}






 main(int argc, char* argv[])
{
int i=0;
    while(1)
{

    i=i++;
    if (i==60)
{
readxml();
    cm->readCommLinkLayer_Reg("","");
    i=0;
}
   Sleep(1000);
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值