Kyle Ramos Kyle Ramos - 2 years ago 245
C Question

C/ HTML - Printing the username and password from an HTML Form

We were asked to create a login page using HTML, C/CGI and CSS. I wanted to start with the basics, HTML and C/CGI first so I created this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
char *data;
char *user;
char *password;
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'>");

However, the compiler is showing me this problem:

warning: format ‘%s’ expects argument of type ‘char *’, but argument 3
has type ‘char **’ [-Wformat=]
sscanf(data,"user=%s&password=%s", &user, &password);

Also, the HTML is showing me Error 500.

Any ideas on how to fix this? I'm fairly new at this HTML/CGI thing, and I would appreciate help along the way. Thanks!

Answer Source

The problem is, sscanf needs a pointer to char:

int sscanf ( const char * s, const char * format, ...);

Try this:

char user[100];
char password[100];
// ...
sscanf(data,"user=%s&password=%s", user, password);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download