Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c and printf

Status
Not open for further replies.

butthead

Programmer
Feb 24, 2002
545
US
I am new to cgi and am having trouble with example programs. All the basic examples that I have come across use a simple printf to display the output.

void main ()
{
printf("Content-type: text/html\n\n");
printf(&quot;<HTML><HEAD><TITLE>Hello!</TITLE></HEAD>\n&quot;);
printf(&quot;<BODY><H1>Hello, World!</H></BODY></HTML>\n&quot;);
}

this wont work on my rig. I have a test example working that I got from This example works but its going to take me a while to go through the code and find what makes it tick. I would like to know why the first code doesnt work. I will probably end up using a third party library but I prefer to have a firm base to start from. basically I need a hello world app that runs on my apache server. none of the simple examlpes that I have found work for me.

platform win98
server apache 2.0

tomcruz.net
 
Have you tried using a standards conformant program and with linebreaks for your OS?
[tt]#include <stdio.h>

int main (int argc, char *argv[])
{
printf(&quot;Content-Type: text/plain\r\n\r\n&quot;);
printf(&quot;Hello world!\r\n&quot;);
return 0;
}[/tt]
works on all my machines.

//Daniel
 
I think the &quot;\r\n\r\n&quot; is used on a nix box
and &quot;\n\n&quot; would be used on bill gates machine.

I tried it yesterday but I tried it again today as you coded it and I still get the same internal server error.


this the error that is logged when I try to invoke this executable from Iexplorer.

Premature end of script headers: proj0001.exe

Im thinking that perhaps the nix os will handle stdout differently than the windoz os and the examples I am getting are targetted for the nix. the executable I did get to work seems to have an added translation layer that hands off the stdout to the server. I guess I will have to spend some time on the code that I have. Im just trying to get a good grasp of the internals of this aspect of cgi before I jump into perl or php. since I already know c it would seem a place to start.

tomcruz.net
 
It's the other way around. UNIX and it's children uses just a line feed to designate a new line (\n), whilst Windows and it's equivalents uses a carriage return and a line feed (\r\n).

I compiled the program above on my Linux boxes and it worked like a charm. Perhaps binary executables doesn't work with Apache on Windows.

//Daniel
 
Ive been using a singular line feed ie &quot;\n&quot; since I started programining in the dos win environment for the printf function. that is not the issue as ive done it both ways to no avail. as I said before I do have an exmple that does work. though it is just 3000 lines of code and will take me a time to understand it. I have noticed that i have somehow screwd up the setup for apache and some perl apps dont work either. It would seem that some do and some dont either in perl or compiled binary. The printenv.pl app that comes with the apache installation does not work now but some other apps work. I think I will uninstall , reformat or just put a gun to the box and put it out of its misery. wish me luck. Thanks for your time.

tomcruz.net
 
tc.n,
Instead of trying to enforce the knowlege you know already in an area that is &quot;apparently&quot; new...take a leap of faith, and have a look at Perl ... C syntax is good for learning Perl.

The libraries are written
The libraries are tested
They're open source, enough said

You're looking at a number of points of interest in terms of taking on CGI and Apache at the same time, they'll both take a little time, but if you know coding already, that'll get paid back in spades, though I haven't seen (or looked for) too many CGI examples in C.

HTH
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top