[英]CGI- Getting the mean of 10 inputs given by user thru HTML
对于我们的家庭作业,我们必须创建一个 HTML,它使用表单从用户那里获取 10 个输入。 然后,从这 10 个输入中,我们必须创建一个程序来获取 10 个输入的平均值,然后显示结果。 这就是我到目前为止所做的:我们必须首先将这 10 个输入存储在一个数组中
CG代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *data;
int i;
int nums[10];
double sum = 0.0;
double size=0.0;
printf("Content-type:text/html");
printf("<html><body>");
data=getenv("QUERY_STRING");
if(data)
{
for(i=0; i<10; i++)
{
sscanf(data,"nums[i]=%d", &nums[i]);
sum += nums[i];
}
size=sum/10;
printf("The average is %d\n",size);
}
return 0;
}
和 HTML:
<html>
<body>
<form action='/cgi-bin/trust.cgi'>
<a: input type=text value='' name=a />
<b: input type=text value='' name=b />
<c: input type=text value='' name=c />
<d: input type=text value='' name=d />
<e: input type=text value='' name=e />
<f: input type=text value='' name=f />
<g: input type=text value='' name=g />
<h: input type=text value='' name=h />
<i: input type=text value='' name=i />
<j: input type=text value='' name=j />
<input type=submit value='Compute'>
</form>
</body>
</html>
问题:我不断收到“错误 500:服务器错误!脚本头过早结束:trust.cgi”。 这是我第一次编写此类代码,我将不胜感激。
编辑:处理那个错误 500。但现在我的问题是它给了我错误的答案,我需要的平均值? 有什么帮助吗? 谢谢!
500
错误是由不正确的 HTTP 标头引起的。 根据RFC 2616 ,HTTP 响应的标头应通过两个 CRLF 换行符(即"\\r\\n\\r\\n"
)与响应正文分开。 您可以通过将第一个printf()
语句更改为:
printf("Content-Type: text/html\r\n\r\n");
(编辑: CGI 规范没有那么严格——你需要的只是标题和正文之间的空行,所以大概
"\\n\\n"
也可以工作。)
此外,您的解析代码是完全错误的。 您传递给sscanf()
的nums[i]=
匹配字符串将只匹配numsi=
,它不会出现在查询字符串中的任何地方。 此外,您每次都将相同的输入字符串( data
)传递给sscanf()
。 尝试使用strsep()
代替。
最后,您尝试将结果( double size;
)打印为整数( "%d"
)。 您应该改用"%f"
格式说明符。
您的 HTML 并不是真正的 HTML,因此请先将其整理好。
<!DOCTYPE html>
<html>
<head>
<title>Get The Average</title>
</head>
<body>
<form action='/cgi-bin/trust.cgi'>
<input type="text" value='' name="a"></br>
<input type="text" value='' name="b"></br>
<input type="text" value='' name="c"></br>
<input type="text" value='' name="d"></br>
<input type="text" value='' name="e"></br>
<input type="text" value='' name="f"></br>
<input type="text" value='' name="g"></br>
<input type="text" value='' name="h"></br>
<input type="text" value='' name="i"></br>
<input type="text" value='' name="j"></br>
<input type="submit" value='Compute'>
</form>
</body>
</html>
用sscanf()
解析查询字符串可能不是最好的主意,而且您的教授似乎持有相同的意见,正如您所写的。 尽管如此与scanf()
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *data;
int i;
int nums[10] = {0};
double sum = 0.0;
double size = 0.0;
printf("Content-type:text/html\r\n\r\n");
printf("<!DOCTYPE html><html><head><title>Here's The Average</title></head><body>");
// TODO: check if data is not NULL
data = getenv("QUERY_STRING");
printf("%s</br>\n", data);
if (data) {
// That's an abomination. Obviously.
sscanf(data, "a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=%d&h=%d&i=%d&j=%d",
&nums[0], &nums[1], &nums[2], &nums[3], &nums[4], &nums[5],
&nums[6], &nums[7], &nums[8], &nums[9]);
for (i = 0; i < 10; i++) {
sum += nums[i];
}
size = sum / 10;
printf("The average is %f\n", size);
}
printf("</body></html>");
exit(EXIT_SUCCESS);
}
使用strtok
看起来确实好一点:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *data;
char *token;
int i = 0;
int nums[10] = { 0 };
double sum = 0.0;
double size = 0.0;
printf("Content-type:text/html\r\n\r\n");
printf
("<!DOCTYPE html><html><head><title>Here's The Average with strtok</title></head><body>");
// TODO: check if data != NULL
data = getenv("QUERY_STRING");
printf("%s</br>\n", data);
if (data) {
token = strtok(data, "&");
while (token) {
// look for the '='
while (*token != '=') {
token++;
}
// and skip it
token++;
nums[i++] = atoi(token);
// check if we have enough
// the constant "10" should better be #define'd
if (i == 10) {
break;
}
token = strtok(NULL, "&");
}
for (i = 0; i < 10; i++) {
sum += nums[i];
}
size = sum / 10;
printf("The average is %f\n", size);
}
printf("</body></html>");
exit(EXIT_SUCCESS);
}
单独标记字符串是不够的,如果您期望它们,您还必须转换/取消转义 HTTP 转义字符(以%
开头的十六进制值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.