C文件:GETENV(QUERY_STRING)用C CGI
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE *ptr;
char m[200];
char *data = malloc(200);
data=getenv("QUERY_STRING");
sscanf(data,"%s", m);
printf("%s", m);
ptr=fopen("c:/test.txt", "w");
fprintf(ptr, "%s", m);
fclose(ptr);
return 0;
}
// GCC -g print.c -o print.exe
HTML文件:
<html>
<body>
<h2>CGI Server</h2>
<p>
<form action="http://localhost/cgi-bin/print.exe">
<div><label>value: <input name="m" size="10"></label></div>
<div><input type="submit" value="Run"></div>
</form>
</p>
</body>
</html>
如果輸入進入網頁形式是c:/data.txt然後結果是:c%3A%2Fdata.txt
發生了什麼事?爲什麼輸出中的/和:損壞?看起來問題在於QUERY_STRING,因爲getenv(「PATH」)不存在這個問題。
我不是C期望,但我確定有已有的CGI和表單數據解析庫。這是一個比看起來更復雜的領域,所以請不要重新發明輪子! – Quentin 2010-10-12 23:21:27