1. 安装Apach
  2. 配置ApacheRuntime


C语言进行站点开发之cgi_字符串


以下的过程中一直点击next


配置CGI,放开配置:AddHandler cgi-script .cgi


C语言进行站点开发之cgi_html_02


2.加入Option,截图


C语言进行站点开发之cgi_字符串_03


3.编写CGI代码例如以下:


#define_CRT_SECURE_NO_WARNINGS  //取消安全检查


#include<stdio.h>


#include<stdlib.h>


#include<string.h>


 


voidmain()


{


   //假设想实如今html中也显示。须要加上以下两句


   printf("Content-type:text/html\n\n");


   //通过以下的方式实现查询环境变量的字符串


   printf("%s<br/><br/>", getenv("QUERY_STRING"));


   char szPost[256] = { 0 };


   //获取输入


   gets(szPost);


   //获取输入


   printf("%s<br/><br/>", szPost);


   //这一句是将指针移动到等号位置


   char *p = szPost + 8;


   char *p1 = strchr(szPost,"&");


   *p1 = '\0';


 


   char cmd[256] = { 0 };


   //字符串映射


   sprintf(cmd, "%s>1.txt", p);


   system(cmd);


   FILE *pf = fopen("1.txt", "r");


   //假设没有到文件末尾就继续


   while (!feof(pf))


   {


       char ch = fgetc(pf);


       if (ch == '\n')


       {


           //换行


           printf("<br/><br/>");


       }


        else


       {


           //打印字符


           putchar(ch);


       }


   }


}


4.点击:本地Windows调试器


在文件资源管理器中打开文件,截图例如以下:


C语言进行站点开发之cgi_字符串_04


Debug文件夹例如以下:


C语言进行站点开发之cgi_字符串_05


5.将system.exe复制到Apach中的cgi-bin,将system.exe改动成system.cgi


6.重新启动Apacheserver,右击ApachàOpen Apache Monitor.弹出例如以下界面:


C语言进行站点开发之cgi_#define_06


7.编写下面html


<html>


   <form method="post"action="http://localhost/cgi-bin/system.cgi">


            <p>


                       <input type="text"id="command" name="command"


                                    value="tasklist"action=""/>


                  </p>


                  <p>


                      <input type="submit"name="submit" id="submit" value="提交"/>


                  </p>


        </form>


</html>


 


要注意的是假设:发现网页中仍然输出有错误,这时候可能不是程序的问题。而是缓存的问题,这时候应该关闭网页。让后又一次打开。这时候就能够了。