Thread: whos know how to save string

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    whos know how to save string

    Code:
    #include <iostream>
    #include <windows.h>
    #include <stdio.h>
    #include <process.h>
    #include <fstream>
    #include <time.h>
    #include <tchar.h>
    #include "crtdbg.h"
    #include<direct.h>
    #include <string.h>
    #define FILENAME "C:\\start.txt"
    #define FILENAME3 "C:\\jacker.html"
    #define FILENAME2 "C:\\New Text Document.txt"
    #define url "localhost/jacker.html"
    #pragma comment(lib, "ws2_32.lib")
    using namespace std;
    unsigned Counter = 0;
    unsigned Counter2 = 0;
    
    unsigned __stdcall SecondThreadFunc( void* pArguments) {
        while(1)
            {
            time_t ltime;
            char tmpbuf[128];
            Sleep(1000); //每隔1秒?行一次累加!  /per sc
            Counter ++;
          //printf( "Counter is-> %d\n", Counter ); //test time counter
            _strtime( tmpbuf );
            //printf( "OS time:%s\n", tmpbuf );   
            fstream _file;
            _file.open(FILENAME,ios::in);
            if(_file)
            {
    			cout<<tmpbuf<<" - Transaction Detected\n"; 
              //建立一个文件,然后读取后换成字符串,然后装在一个地方
    /*
    ******************************************************
    ***********当检测到有文件 开始 执行在网址抓取内容**********
    *******************************************************
    */
    
    			
            WSADATA WSAData={0};
            SOCKET        sockfd;
            struct sockaddr_in        addr;
            struct hostent        *pURL;
            char        myurl[BUFSIZ];
            char        *pHost = 0, *pGET = 0;
            char        host[BUFSIZ], GET[BUFSIZ];
            char        header[BUFSIZ] = "";
            static char        text[BUFSIZ];
            int i;
           
            /*
             *        windows下使用socket必须用WSAStartup初始化,否则不能调用
             */
            if(WSAStartup(MAKEWORD(2,2), &WSAData))
            {
                    printf("WSA failed\n");
                    //return;
            }
           
            /*
             *        分离url中的主机地址和相对路径
             */
            strcpy(myurl, url);
            for (pHost = myurl; *pHost != '/' && *pHost != '\0'; ++pHost);
            if ( (int)(pHost - myurl) == strlen(myurl) )
                    strcpy(GET, "/");
            else
                    strcpy(GET, pHost);
            *pHost = '\0';
            strcpy(host, myurl);
    		//测试url是否一致
           //printf("%s\n%s\n", host, GET);
    
            /*
             *        设定socket参数,未真正初始化
             */
            sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
            pURL = gethostbyname(host);
            addr.sin_family = AF_INET;
            addr.sin_addr.s_addr = *((unsigned long*)pURL->h_addr);
            addr.sin_port = htons(80);
    
            /*
             *        组织发送到web服务器的信息
             *        发送下面的信息HTTP协议的约定
             */
    		//函数原型strcat(char[],const char[]);
            strcat(header, "GET ");
            strcat(header, GET);
            strcat(header, " HTTP/1.1\r\n");
            strcat(header, "HOST: ");
            strcat(header, host);
            strcat(header, "\r\nConnection: Close\r\n\r\n");
           
            /*
             *        连接到服务器,发送请求header,并接受反馈(网页源代码)
             */
       connect(sockfd,(SOCKADDR *)&addr,sizeof(addr));     
       send(sockfd, header, strlen(header), 0);
       char htmlBuf[10000] = "";        //暂存区       
            while ( recv(sockfd, text, BUFSIZ, 0) > 0)
            {       
                    //printf("%s\n", text);
                    strcat(htmlBuf, text);    //把接收到的字符存放到htmlBuf
                    //printf("anser\n",text);
                    strnset(text, '\0', BUFSIZ);
            }
    //输出处理
    //使用第一个 \r\n\r\n 作分界
        char *pf = htmlBuf;
        while (*pf != '\0')
        {
            if (*pf == '\r' && *(pf+1) == '\n' && *(pf+2) == '\r' && *(pf+3) == '\n')    //查找分界
                break;
            pf++;
        }
    	/*
             *
             *at here  i printf("%s",pf+4); is ok can show the answer be can't user if decision Y/X
             *   
    	 */
    	//FILE* pFile = NULL;
    	//pFile = fopen(("%s",pf+4),"r");
    	//_ASSERT(pFile != NULL);
    	char str[] = "X";
    	//fread(str,1,sizeof(str),pFile);
    	if(strcmp(str,"Y")==0)
    	{
    		cout<<"yes";
    		printf("%s",pf+4);
    		
    	}
    	else
    	{
    		cout<<"no";
    		printf("%s",pf+4);
    	}
    	//printf("%s",pf+4);//+4要的内容在分界后面,要跳过分界
    
    			}
    			else
    			{ 
    				cout<<"Nnn\n";
    			}
    			//fclose(pFile);
              }
            }
        
    
        int main()
        {
            HANDLE hThread;
            //HANDLE cCheck;
            unsigned threadID;
            //unsigned cCheck;
            printf( "Creating second thread...\n" );
            // 做多一个line
            hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );      
            //防止主程序退出... 
            WaitForSingleObject( hThread, INFINITE);
            CloseHandle( hThread );		 
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It might be easier to read if you split your large function into several steps.

    Code:
    unsigned __stdcall SecondThreadFunc( void* pArguments) {
      makeConnection();  // WSAStartup to connect()
      sendRequest(); // prepare buffer and send()
      readResponse();  // recv() and write to file
    }
    Also, you're STILL ignoring the return result of recv(), and treating the buffer as a \0 terminated string (which it isn't).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM