当我试图读取字符串时,我得到一个错误形式sscanf。获取字符串的某一部分。我从一个HTML文件得到字符串,我使用CGI作为服务器端从表单读取。
<!DOCTYPE html>
<html>
<head>
<title>CGI-zoo</title>
<style>
body {
text-align: center;
}
#nameErrorMsg {
color: red;
}
#title {
color: orange;
}
#animalDiv {
border-style: groove;
}
</style>
</head>
<body>
<script>
//global variable for name
var name;
//function :check()
//Parameters : void
//Returns : void
//Description : This function will make sure that the user enters something for the name and does not leave the text box blank
function checkName()
{
name= name=document.getElementById("nameEntered").value;
if(name==""||name==undefined)
{
//error msg
document.getElementById("nameErrorMsg").innerHTML="This text box can not be blank";
return false;
}
else {
//hide the name text box and button
document.getElementById("nameDiv").hidden=true;
//unhides the drop list
document.getElementById("askingUserWhatAnimal").innerHTML="Hello "+name+ ", Please choose one of the six animals to learn some facts from";
document.getElementById("animalDiv").hidden=false;
//unhide the submit form button
document.getElementById("submitForm").hidden=false;
return true;
}
}
</script>
<form id="cgiZoo" action="animal.exe" method="get">
<h1 id="title"> cgi-Zoo</h1>
<hr>
<div id="nameDiv">
<p id="nameErrorMsg"></p>
<label id="nameLabel">Name:</label>
<input type="text" id="nameEntered" placeholder="Name" name="nameEntered" v>
<button type="button" onclick="checkName()" id="nameCheckBtn">Submit </button>
</div>
<!---Animal drop down list -->
<div hidden id="animalDiv">
<p id="askingUserWhatAnimal"></p>
<label>Animals</label>
<select name="animalList" id="animalList" required="required">
<option></option>
<option value="Lion">Lion</option>
<option value="Elephant">Elephant</option>
<option value="Giraffe">Giraffe</option>
<option value="Moneky">Moneky</option>
<option value="Zebra">Zebra</option>
<option value="Panda">Panda</option>
</select>
<br>
<br>
</div>
<br>
<input hidden type="submit" id="submitForm" form="cgiZoo" name="submit">
</form>
</body>
</html>
当你提交这个表单时,它应该带你到一个服务器端脚本,基本上告诉用户他们输入了什么名字,他们使用CGI选择了什么动物。
但是我遇到了一个问题,试图从使用sscanf
的数据中读取和拼接字符串使用c++编写的CGI代码
#include <iostream>
#include <string.h>
#pragma warning(disable: 4996)
#define CGI_VARS 30
#define SEND_METHOD 19 // REQUEST_METHOD tells us if the form was sent using GET or POST
#define DATA_LENGTH 14
using namespace std;
int main()
{
cout << "Content-type:text/htmlrnrn";
cout << "<html>n";
cout << "<head>n";
cout << "<title>CGI Environment Variables</title>n";
cout << "</head>n";
cout << "<body>n";
char* data;
char* name;
char *animal;
//returns pointer
data = getenv("QUERY_STRING");
if (sscanf(data, "nameEntered=%s&animalName=%s", name, animal));
cout << name;
cout << animal;
cout << "</body>";
return 0;
}
非常感谢您的帮助。我不知道我做错了什么。下面我将显示输出结果。
输出:
name="bob"
animal="lion"
name
和animal
不指向您已分配的任何内存。一个简单的修复方法是将它们变成char[]
s:
char name[256];
char animal[256];
char* data = getenv("QUERY_STRING");
// check that data is non-null and check the sscanf result:
if(data && sscanf(data, "nameEntered=%255[^&]&animalName=%255[^&]", name, animal) == 2)
{ // ^^^ ^^^
// limit the scan to the size of the char[] - 1
std::cout << "name="" << name << ""nnanimal="" << animal << ""n";
}
注意%[^&]
而不是%s
的扫描。它一直扫描到但不包括&
。