To save you some future debugging / headaches.
1) you have to have proper http headers *before* the data is output and they contain the number of characters in the output. Therefore, you need to "buffer up" all your output inside the application before finally dumping it to 'stdout'
2) C/C++ Newline ('\n') will only count as 1 character in a buffer but will become two characters when you print it on 'stdout' so count the number of newlines separately and add that to the length of the buffered output.
Below is the bottom of my CGI app. I buffer the output into
total_printout
and count newlines in
total_newlines
. How you do your output buffering and the like is up to you.
printf("Content-type: text/html\n"); if (total_printout != NULL)
{
printf("Content-length: %d\n\n", strlen(total_printout)+total_newlines);
printf("%s", total_printout); free(total_printout); }
else
{
printf("Content-length: 0\n\n"); }