- 论坛徽章:
- 0
|
请教:用c++写的cgi接受到的中文字符为什么是乱码?
int main()
{
char postString[ 1024 ] = "";
int contentLength = 0;
// variables to store user data
string dataString = "";
string name = "";
string email = "";
string content = "";
string getUserIp = "";
string getNowTime = "";
time_t currentTime;
// data was posted
if ( getenv( "CONTENT_LENGTH" ) )
contentLength = atoi( getenv( "CONTENT_LENGTH" ) );
cin.read( postString, contentLength );
dataString = postString;
// search for first '+' character
int charLocation = dataString.find( "+" );
// search for next '+' character
while ( charLocation < string::npos ) {
dataString.replace( charLocation, 1, " " );
charLocation = dataString.find( "+", charLocation + 1 );
} // end while
// find location of tname
int firstStart = dataString.find( "name=" ) + 5;
int endFirst = dataString.find( "&email" );
name = dataString.substr( firstStart,
endFirst - firstStart );
// find location of content
int lastStart = dataString.find( "content=" ) + 8;
int endLast = dataString.find( "&submit" );
content = dataString.substr( lastStart,
endLast - lastStart );
content = HexToChar(content);
// find location of e-mail address
int emailStart = dataString.find( "email=" ) + 6;
int endEmail = dataString.find( "&content" );
email = dataString.substr( emailStart,
endEmail - emailStart );
//
getUserIp = getenv("REMOTE_ADDR" ;
time(¤tTime);
getNowTime = asctime( localtime( ¤tTime ) );
// output header
cout << "Content-type: text/html\n\n";
cout << "<?xml version = \"1.0\"?>;"
<< "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 "
<< "Transitional//EN\" \"http://www.w3.org/TR/xhtml1"
<< "/DTD/xhtml1-transitional.dtd\">;";
cout << "<html xmlns = \"http://www.w3.org/1999/xhtml\">;"
<< "<head>;<title>;Contact Information entered"
<< "</title>;</head>;<body>;";
if ( name == "" or content == "" {
cout << "错误:请您将数据填写完整。";
return 0;
}
// output to file
ofstream outFile( "clients.txt", ios::app );
if ( !outFile ) {
cout << "错误:没有文件操作权限";
return 0;
}
outFile << name << "\t" << email << "\t" << content << " \n";
// output data to user
cout << "发表成功<br>;<table>;<tbody>;"
<< "<tr>;<td>;Name:</td>;<td>;"
<< name << "</td>;</tr>;"
<< "<tr>;<td>;email:</td>;<td>;"
<< email << "</td>;</tr>;"
<< "<tr>;<td>;content:</td>;<td>;"
<< content << "</td>;</tr>;"
<< "<tr>;<td>;ip:</td>;<td>;"
<< getUserIp << "</td>;</tr>;"
<< "<tr>;<td>;time:</td>;<td>;"
<< getNowTime << "</td>;</tr>;"
<< "</tbody>;</table>;<p align=center>;<A HREF=\"guest.cgi\">;返回</A>;</p>;"
<< "</body>;\n</html>;\n";
return 0;
} // end main |
|