Can't get sprintf, strcat and char arrays working together

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MimiMi
    New Member
    • Aug 2007
    • 42

    Can't get sprintf, strcat and char arrays working together

    I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly!

    I wouldn't be surprised if my problems have something to do with my still existing confusion over arrays and pointers in C since I'm still a newbie, but anyhow, here's the code:

    Code:
    #include <time.h>
    #include <stdio.h>
    
    #define WEBBUF_SIZE 32768
    
    int webbuflen;
    char webbuf[WEBBUF_SIZE];
    
    static char OKheader[] = "HTTP/1.1 200 OK\r\n";
    static char texthtmlheader[] = "Connection: close\r\nContent-Type: text/html\r\n";
    static char contentLenheader[] = "Content-Length: ";
    
    static char data[] = "<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>\n";
    
    int main()
    { 
      http_response();
      return 0;
    }
    
    int getTimeStamp(unsigned char* timestring)
    { 
      struct tm *ptr;
      time_t tm;
      tm = time(NULL);
      ptr = localtime(&tm);
      strftime(timestring ,100 , "Date: %a, %d %b %Y %H:%M:%S GMT\r\n",ptr);
      return 0;
    }
    
    int http_response()
    {
      char tmp[10];
      char datestring[60];
      char header[1048];
    
      //Get content-length (length of data) as a string
      sprintf(tmp,"%d%s",(strlen(data)+1),"\r\n\r\n");
      strcat(contentLenheader,tmp);
    
      getTimeStamp(datestring);
    
      //write everything to header
      sprintf(header, "%s%s%s%s", OKheader,datestring, texthtmlheader,contentLenheader);
    
      //Appending data to header
      strcat(header,data);
    
      //Adding header to webbuf
      webbuflen = sprintf(webbuf, "%s",header);
      
      printf("\nWebbuflen is now: %d",webbuflen);
      printf("\nAnd webbuf is: \n\n%s",webbuf);
      
      return 0;
    }
    I get this printout when I run this program:

    Webbuflen is now: 123
    And webbuf is:

    HTTP/1.1 200 OK
    Date: Fri, 22 Feb 2008 09:14:59 GMT
    Connection: close
    Content-Type: text/html
    Content-Length: 93


    But I would like to get:

    HTTP/1.1 200 OK
    Date: Fri, 22 Feb 2008 09:14:59 GMT
    Connection: close
    Content-Type: text/html
    Content-Length: 93

    <html><head><ti tle>Testing</title></head><body>Test ,test,test, testing, test.</body></html>

    Any help or pointers in the right direction is greatly appreciated!
    Cheers!
  • arthurshen
    New Member
    • Feb 2008
    • 8

    #2
    Originally posted by MimiMi
    I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly!

    I wouldn't be surprised if my problems have something to do with my still existing confusion over arrays and pointers in C since I'm still a newbie, but anyhow, here's the code:

    Code:
    #include <time.h>
    #include <stdio.h>
    
    #define WEBBUF_SIZE 32768
    
    int webbuflen;
    char webbuf[WEBBUF_SIZE];
    
    static char OKheader[] = "HTTP/1.1 200 OK\r\n";
    static char texthtmlheader[] = "Connection: close\r\nContent-Type: text/html\r\n";
    static char contentLenheader[] = "Content-Length: ";
    
    static char data[] = "<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>\n";
    
    int main()
    { 
      http_response();
      return 0;
    }
    
    int getTimeStamp(unsigned char* timestring)
    { 
      struct tm *ptr;
      time_t tm;
      tm = time(NULL);
      ptr = localtime(&tm);
      strftime(timestring ,100 , "Date: %a, %d %b %Y %H:%M:%S GMT\r\n",ptr);
      return 0;
    }
    
    int http_response()
    {
      char tmp[10];
      char datestring[60];
      char header[1048];
    
      //Get content-length (length of data) as a string
      sprintf(tmp,"%d%s",(strlen(data)+1),"\r\n\r\n");
      strcat(contentLenheader,tmp);
    
      getTimeStamp(datestring);
    
      //write everything to header
      sprintf(header, "%s%s%s%s", OKheader,datestring, texthtmlheader,contentLenheader);
    
      //Appending data to header
      strcat(header,data);
    
      //Adding header to webbuf
      webbuflen = sprintf(webbuf, "%s",header);
      
      printf("\nWebbuflen is now: %d",webbuflen);
      printf("\nAnd webbuf is: \n\n%s",webbuf);
      
      return 0;
    }
    I get this printout when I run this program:

    Webbuflen is now: 123
    And webbuf is:

    HTTP/1.1 200 OK
    Date: Fri, 22 Feb 2008 09:14:59 GMT
    Connection: close
    Content-Type: text/html
    Content-Length: 93


    But I would like to get:

    HTTP/1.1 200 OK
    Date: Fri, 22 Feb 2008 09:14:59 GMT
    Connection: close
    Content-Type: text/html
    Content-Length: 93

    <html><head><ti tle>Testing</title></head><body>Test ,test,test, testing, test.</body></html>

    Any help or pointers in the right direction is greatly appreciated!
    Cheers!
    Dear:MimiMi

    Increasing your contentLenheade r[] size.Then you will find out your answer.Good luck :)

    Comment

    • MimiMi
      New Member
      • Aug 2007
      • 42

      #3
      Originally posted by arthurshen
      Dear:MimiMi

      Increasing your contentLenheade r[] size.Then you will find out your answer.Good luck :)
      Thank you so much! That solved the problem!
      I still don't understand why though.. Do you (or anybody else) mind trying to explain why my solution didn't work, please :) ?

      Comment

      • arthurshen
        New Member
        • Feb 2008
        • 8

        #4
        Originally posted by MimiMi
        Thank you so much! That solved the problem!
        I still don't understand why though.. Do you (or anybody else) mind trying to explain why my solution didn't work, please :) ?
        Dear:MimiMi

        First,you need to know how array initiated in C.

        Initiated automatically by compiler.It's look like your

        solution.Or declare array by yourself.

        What's difference between them ???

        Then research strcat() function,and you will getting

        something wrong on line 39 in your codes.

        Finally,I am glad to answer your question.It's also help

        me to practice me English.ha ha~~ :)

        (Wish you understand what I say) 冏rz

        Comment

        • MimiMi
          New Member
          • Aug 2007
          • 42

          #5
          Hi and thanks again for taking the time! I think I understand it a little bit better now, so again, thank you!

          Cheers!

          Comment

          Working...