#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
//輸出HTTP頭
printf("Content-type: text/plain\n\n");
//從CONTENT_LENGTH環境變量中取出其值
int n = atoi(getenv("CONTENT_LENGTH"));
printf("%d\n", n);
char buf[20];
memset(buf, 0, sizeof(buf));
//從stdin標準輸入流中讀取數據
fgets(buf, n, stdin);
//輸出
printf("Hi, this is CGI with c\n");
printf("%s\n", buf);
return 0;
}
大致流程妳都懂,就是有些細節不清楚吧。從stdin讀取數據時,首先要知道讀多長的數據,而這個長度保存在“CONTENT_LENGTH‘整個環境變量中。另外從stdin讀取數據要用fgets函數,scanf不是用在網絡通信中讀取數據用的。