我们被要求使用 HTML、C/CGI 和 CSS 创建登录页面。我想从基础知识开始,首先是 HTML 和 C/CGI,所以我创建了这个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *data;
char *user;
char *password;
printf("Content-type:text/html\r\n\r\n");
printf("<!DOCTYPE html><html><head><title>Welcome!</title></head><body>");
data = getenv("QUERY_STRING");
if (data) {
sscanf(data,"user=%s&password=%s", &user, &password);
printf("Hello Mr./Ms. %s\n",user);
printf("You entered your password %s\n",password);
}
printf("<form action='http://localhost/inch.html'>");
printf("<input type=submit value='Compute'>");
printf("</body></html>");
exit(EXIT_SUCCESS);
}
但是,编译器向我展示了这个问题:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char **’ [-Wformat=] sscanf(data,"user=%s&password=%s", &user, &password);
此外,HTML 显示错误 500。
关于如何解决这个问题有什么想法吗?我在 HTML/CGI 方面还很陌生,我很感激一路上的帮助。谢谢!
最佳答案
问题是,sscanf需要一个指向 char 的指针:
int sscanf ( const char * s, const char * format, ...);
试试这个:
char user[100];
char password[100];
// ...
sscanf(data,"user=%s&password=%s", user, password);
关于html - C/HTML - 从 HTML 表单打印用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40242667/