CGI with C

I am going to test a CGI-script. I've written two files:
1) test.html
<META NAME="GENERATOR" Content="Microsoft Developer Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>

<!-- Insert HTML here -->
<form method=get action="test.cgi">
Enter some text here:<br>
<input type="text" name="sometext" value="">
<input type="submit" value="Send">


2) test.c
#include <stdio.h>
#include <stdlib.h>

void header(), footer();

int main()
      printf("Querry String: %s\n\n", getenv("QUERY_STRING"));

      return 0;

void header()
      printf("Content-type: text/html\n\n");
      printf("<head>\n<title>Example CGI Output</title>\n</head>\n");

void footer()

I build the c-fiel successed and get "test.exe". I change it to "test.cgi". Then I haven't permit to our server, so I save the files under the same directory.

I open the "test.html" with Netscape browser. I tip some text in the input window. But as I click the "Sene"-button, it shows some strange text which I can't understand.

How can I solve the problem?

chen wei

Who is Participating?
julio011597Connect With a Mentor Commented:
Yes chenwei, CGI is a protocol stating how a web server can communicate with an external program; i.e., no server... no CGI.

To test your scripts locally, you should install a web server on your PC and work with it (you can download a web server for free from both Netscape and Microsoft).

Anyway, as Michel says, you could first try to talk to your Internet Provider and see if they can help you.
chenweiAuthor Commented:
Edited text of question
chenweiAuthor Commented:
Edited text of question
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Your program should show something like:


with sequences of "%xx" (where xx is the ASCII code in hexadecimal) replacing any non-printable chars.

If this is what you get, then this is how CGI works.
You need your string parsing routines to exctract and decode key-value pairs from the query string.

BTW, the getenv() function can fail and return NULL, so you should do something like:

int main(void) {
  char *query_string = getenv("QUERY_STRING");


  if(query_string) printf("Query string: %s\n", query_string);
  else puts("Cannot get query string!");


  return 0;

chenweiAuthor Commented:
First I would like to thank julio's answer. But it dosen't help.
I was told (and I am sure) that the files I wrote are correct. The problem may be that I should save the cgi-file(test.cgi) under the directory:/cgi-bin by the server, then the cgi-script can only be executed by server not by client. But I haven't the access to the server.

I do want to test the cgi-scripts I have written, but I don't know where and how.
I think you should ask your provider for a solution for this problem. He can give you access to your cgi-bin directory.
All Courses

From novice to tech pro — start learning today.