473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

42
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:

Expand|Select|Wrap|Line Numbers
  1. #include <time.h>
  2. #include <stdio.h>
  3.  
  4. #define WEBBUF_SIZE 32768
  5.  
  6. int webbuflen;
  7. char webbuf[WEBBUF_SIZE];
  8.  
  9. static char OKheader[] = "HTTP/1.1 200 OK\r\n";
  10. static char texthtmlheader[] = "Connection: close\r\nContent-Type: text/html\r\n";
  11. static char contentLenheader[] = "Content-Length: ";
  12.  
  13. static char data[] = "<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>\n";
  14.  
  15. int main()
  16.   http_response();
  17.   return 0;
  18. }
  19.  
  20. int getTimeStamp(unsigned char* timestring)
  21.   struct tm *ptr;
  22.   time_t tm;
  23.   tm = time(NULL);
  24.   ptr = localtime(&tm);
  25.   strftime(timestring ,100 , "Date: %a, %d %b %Y %H:%M:%S GMT\r\n",ptr);
  26.   return 0;
  27. }
  28.  
  29. int http_response()
  30. {
  31.   char tmp[10];
  32.   char datestring[60];
  33.   char header[1048];
  34.  
  35.   //Get content-length (length of data) as a string
  36.   sprintf(tmp,"%d%s",(strlen(data)+1),"\r\n\r\n");
  37.   strcat(contentLenheader,tmp);
  38.  
  39.   getTimeStamp(datestring);
  40.  
  41.   //write everything to header
  42.   sprintf(header, "%s%s%s%s", OKheader,datestring, texthtmlheader,contentLenheader);
  43.  
  44.   //Appending data to header
  45.   strcat(header,data);
  46.  
  47.   //Adding header to webbuf
  48.   webbuflen = sprintf(webbuf, "%s",header);
  49.  
  50.   printf("\nWebbuflen is now: %d",webbuflen);
  51.   printf("\nAnd webbuf is: \n\n%s",webbuf);
  52.  
  53.   return 0;
  54. }
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><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>

Any help or pointers in the right direction is greatly appreciated!
Cheers!
Feb 22 '08 #1
4 3048
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:

Expand|Select|Wrap|Line Numbers
  1. #include <time.h>
  2. #include <stdio.h>
  3.  
  4. #define WEBBUF_SIZE 32768
  5.  
  6. int webbuflen;
  7. char webbuf[WEBBUF_SIZE];
  8.  
  9. static char OKheader[] = "HTTP/1.1 200 OK\r\n";
  10. static char texthtmlheader[] = "Connection: close\r\nContent-Type: text/html\r\n";
  11. static char contentLenheader[] = "Content-Length: ";
  12.  
  13. static char data[] = "<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>\n";
  14.  
  15. int main()
  16.   http_response();
  17.   return 0;
  18. }
  19.  
  20. int getTimeStamp(unsigned char* timestring)
  21.   struct tm *ptr;
  22.   time_t tm;
  23.   tm = time(NULL);
  24.   ptr = localtime(&tm);
  25.   strftime(timestring ,100 , "Date: %a, %d %b %Y %H:%M:%S GMT\r\n",ptr);
  26.   return 0;
  27. }
  28.  
  29. int http_response()
  30. {
  31.   char tmp[10];
  32.   char datestring[60];
  33.   char header[1048];
  34.  
  35.   //Get content-length (length of data) as a string
  36.   sprintf(tmp,"%d%s",(strlen(data)+1),"\r\n\r\n");
  37.   strcat(contentLenheader,tmp);
  38.  
  39.   getTimeStamp(datestring);
  40.  
  41.   //write everything to header
  42.   sprintf(header, "%s%s%s%s", OKheader,datestring, texthtmlheader,contentLenheader);
  43.  
  44.   //Appending data to header
  45.   strcat(header,data);
  46.  
  47.   //Adding header to webbuf
  48.   webbuflen = sprintf(webbuf, "%s",header);
  49.  
  50.   printf("\nWebbuflen is now: %d",webbuflen);
  51.   printf("\nAnd webbuf is: \n\n%s",webbuf);
  52.  
  53.   return 0;
  54. }
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><title>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 contentLenheader[] size.Then you will find out your answer.Good luck :)
Feb 22 '08 #2
MimiMi
42
Dear:MimiMi

Increasing your contentLenheader[] 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 :) ?
Feb 22 '08 #3
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
Feb 23 '08 #4
MimiMi
42
Hi and thanks again for taking the time! I think I understand it a little bit better now, so again, thank you!

Cheers!
Feb 26 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ian Stanley | last post by:
Hi, Having not done any C programming for a while I am trying to get back into it by converting an old java assignment into C. The problem is I am getting a segmentation fault at runtime which I...
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
44
by: Nicolas | last post by:
On most implementations of the standard C library, the functions that copy characters in a previously allocated buffer, like strcpy or strcat, are returning the pointer to that buffer. On my...
14
by: Stegano | last post by:
I am learning C Programming after working with Java for 5 years. I want to know where can I find the source files for C language itself. For example strcat is a function, which concatenates two...
24
by: diego.arias.vel | last post by:
Hi all I have certain problem when I'm doing this: void copy(char *filename) { char *log_file; log_file=filename; strcat(log_file,"-log.txt");
20
by: ramubdvt | last post by:
hi, i have written this strcat but sometime it is giving problem, while handeling some strings containing binary and if string containing zero , funtion which takes string 1 and its length...
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
66
by: gyan | last post by:
Hi All, I am using sprintf and getting starnge output in following case char temp_rn; memset(temp_rn,'\0',12); sprintf(temp_rn,"0%s",temp_rn); the final value in temp_rn is 00 how it...
33
by: insirawali | last post by:
I have written this in C#. I just need to convert it into C. da problem comes when handling the String. In C the String doesn't behave the same way as in C#. it would be great help if...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.