gpt4 book ai didi

html - 从 HTML 表单中使用 c 程序获取数据

转载 作者:太空宇宙 更新时间:2023-11-04 04:03:48 25 4
gpt4 key购买 nike

我创建了以下 C 程序来从 HTML 表单中获取数据。但是当我尝试编译并运行它时,我得到:

segmentation fault 11 (core dumped)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
char *N1,*N2,*N3,*N4,*N5;
int cgi_length;
char *cgi_data;
printf("Content-type:text/html\n\n");
cgi_length=atoi(getenv("CONTENT_LENGTH"));
cgi_data=malloc(cgi_length+1);
fread(cgi_data,1,cgi_length,stdin);
printf("<HTML>");
printf("<HEAD><TITLE>DATA</TITLE></HEAD><BODY>\n");
printf("<H3>DATA</H3>\n");
if(cgi_data == NULL)
{
printf("<P>Error! Error in passing data from form to script.");
}
else {
printf("%s",cgi_data);
sscanf(cgi_data,"N1=%s&N2=%s&N3=%S&N4=%SN5=%s",&N1,&N2,&N3,&N4,&N5);
printf("<P>N1 is %s and N2 is %s and N3 is %S and N4 is %S and N5 is %s.",N1,N2,N3,N4,N5);
}
}

另外,如果我使用 ls命令查看 cgi-bin 目录中的数据 我看到创建了一个名为 myprogram.cgi.core 的文件。

谁知道哪里出了问题?

最佳答案

我之前遇到过同样的问题。我在 while 循环中使用了 fgetsfread 对我不起作用。试试这个:

cgi_length=atoi(getenv("CONTENT_LENGTH"));
cgi_data=malloc(cgi_length+1);
while(fgets(cgi_data,cgi_length,stdin)!=NULL){
//insert some processing here
}

关于html - 从 HTML 表单中使用 c 程序获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8679733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com