cgi程序网页显示乱码解决方法

cgi程序网页显示乱码
写了个cgi程序用index.html调用cgi程序后网页前面出现大量乱码,中间显示cgi程序内容,后半段又出现一大片乱码。这是怎么回事,怎么解决呢?用火狐直接打开cgi程序的话提示是二进制文件,是否下载。
cgi程序如下:
#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
int cgiMain() {

cgiHeaderContentType("text/html");
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY><H1>hello word</H1>\n");
fprintf(cgiOut, "</BODY></HTML>\n");
return 0;
}
html文件如下:
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="cgi-bin/test.cgi" method="get">
<input type="text" name="theText"  />
<input type="submit" value="Continue &rarr;" />
</form>
</body>
</html>
求高人指点呀
------解决方案--------------------
我也是遇到了这种情况,我是在ubuntu 10。04 虚拟机上 搭建个 apache2 服务器。
cgi如下:
#include <stdio.h>
int main()
{
printf("Content-type: text/html\n");
printf("\n");
printf("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=gb2312\"><title>A small CGI program</title></head>\n");
printf("<body><center><h1>Hello world!</h1></center></body></html>");
return 0;
}
用shell写的
#!/bin/sh
echo 'Content-type: text/html'
echo
echo "<html><head><title>A small CGI program</title></head>"
echo "<body><center><h1>Hello world!</h1></center></body></html>"