Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am in the process of building my first website and have decided to write my own cgi for form handling and browser response. I know that in windows programming output is directed to a window and in console programming output is directed to the console. To what does one direct output meant for the browser when writing a cgi?
Posted

Just print to the console, and remember to set the right Content-Type.
 
Share this answer
 
v2
Comments
Gbenbam 2-Nov-11 11:58am    
You mean the MIME ?
Nish Nishant 2-Nov-11 12:04pm    
No, the HTTP headers.
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.

C
printf("Content-type: text/html\n");        // make receiving browser happy
if (total_printout != NULL)
{
    printf("Content-length: %d\n\n", strlen(total_printout)+total_newlines);
    printf("%s", total_printout);           // now send it
    free(total_printout);                   // return accumulated output
}
else
{
    printf("Content-length: 0\n\n");        // no output
}
 
Share this answer
 
v2
Comments
Gbenbam 27-Dec-11 12:47pm    
From what does a cgi program read an html for input? I know cgi programs direct output to the console. But I wish to know from what it gets its form input from browser. It certainly cannot be the keyboard.

Where can I get a sample code of cgi getting form input?

Athough this is not programmming related, do you by chance know the answer to the following questions?
1. How does one e-mail ann e-mail form in HTML. By this I mean what will the action attribute form an e-mail foirm be set to.Will it be a mailto:\\username@DomainnName.com I just want o know how to include e-mail sending to my website.

2.How can I make my website receive e-mail. Where do I get a sample cgi program that handles reception of e-mails. How is coding for e-mail reception done?

3.Where can I get a sample server-side image-map map program with explanation on how it receives input from browser. How is coding for server-side image map done. How does a server-side image map file look like.

4. Where can I get a sample code for downloading/uploading files from/to a remote server computer in a network when
1. The computers belong to the same domain;
2. the computers belong to the same work group.
Chuck O'Toole 27-Dec-11 12:57pm    
Sorry pal, you'll have to ask those to the general forum. I don't have any quick pointers to documents for you and you can search for them just as easily as I could.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900