1

我有个问题。我收到来自服务器的响应作为字符数组。例如:555#200#。我将数组拆分为555and200因为那是我的命令。我想将该命令保存到另一个数组,但我得到了不好的结果。这是我的代码:

void READ_FROM_SERVER(void) {
    int read_vkluci = read(sock_descriptor, read_from_server, sizeof(read_from_server));

    if (read_vkluci < 0)
        printf("Failed reading  bytes from server\n");

    printf(" %s \n", read_from_server);

    if(read_vkluci>0){
        char** tokens;
        printf("naredbi=%s \n\n", read_from_server);
        tokens = str_split(read_from_server, '#');

        if (tokens){
            int i;

            for (i = 0; *(tokens + i); i++){
                printf("naredba=%s\n", *(tokens + i));
                naredbi_odelno[i]=  *(tokens + i);
                printf("naredba_odelno=%s\n", naredbi_odelno[i]);
                brojnacomandi++;
                //free(*(tokens + i));
            }

            printf("\n");
            //free(tokens);
        }
    }

    int p;
    for(p=0;p<brojnacomandi;p++){

        if (naredbi_odelno[p]==201) {
            nadvoresna_komanda = VKLUCI;
            printf("VKLUCEN UVLEKUVAC!!!!");
        }
        if (naredbi_odelno[p]==200) {
            nadvoresna_komanda = ISKLUCI;
            printf("ISLKUCEN UVLEKUVAC!!!!");
        }

        if (naredbi_odelno[p]==96) {
            printf("TCP_CONN_OK!!!\n");
        }
        if (naredbi_odelno[p]==211) {
            makeTXpaket(STACK_1);
            p_tx_buffer_ = &tx_buffer[1];
            nbytes = write(fd, tx_buffer, *p_tx_buffer_);
            if (nbytes != tx_buffer[1]) {
                /* problem! */
                printf("error writing on serial port!!!");
            }
            printf("PRIMAM....\n");
            isprateno=0;
        }

        if (naredbi_odelno[p]==210) {
        makeTXpaket( RETURN);
        p_tx_buffer_ = &tx_buffer[1];
        nbytes = write(fd, tx_buffer, *p_tx_buffer_);
        if (nbytes != tx_buffer[1]) {
                /* problem! */
                printf("error writing on serial port!!!");
        }
        printf("VRAKAM....\n");
        isprateno=0;
        }
        //  else  {
        //      printf("TCP_NOT_OK!!!!\n");
        //      close(sock_descriptor);
        //      CONECT_T0_SERVER();
        //  }
        Clean_read_from_server_buff();
        Clean_naredbi_odelno();
    }
}

char** str_split(char* a_str, const char a_delim){
    char** result    = 0;
    size_t count     = 0;
    char* tmp        = a_str;
    char* last_comma = 0;

    /* Count how many elements will be extracted. */
    while (*tmp){
        if (a_delim == *tmp){
            count++;
            last_comma = tmp;
        }
        tmp++;
    }

    /* Add space for trailing token. */
    count += last_comma < (a_str + strlen(a_str) - 1);

    /* Add space for terminating null string so caller
       knows where the list of returned strings ends. */
    count++;

    result = malloc(sizeof(char*) * count);

    if (result){
        size_t idx  = 0;
        char* token = strtok(a_str, "#");

        while (token){
            assert(idx < count);
            *(result + idx++) = strdup(token);
            token = strtok(0, "#");
        }
        assert(idx == count - 1);
        *(result + idx) = 0;
    }

    return result;
}

服务器只发给我 555#,但我得到了naredba_odelno不好的结果!知道为什么会这样吗?

我怎么做naredba_odelno=naredba因为naredba很好!

输出:

naredbi=555#

naredba=555

naredbi_odelno=……成绩不好为什么?

4

1 回答 1

0

我的第一个猜测是您正在读取输入数据的结尾。请注意,read它不会终止字符串,因此您的printf语句会立即被破坏。

尝试这个:

int read_vkluci = read(sock_descriptor, read_from_server, sizeof(read_from_server)-1);

和这个

read_from_server[read_vkluci] = '\0';
printf("naredbi=%s \n\n", read_from_server);

顺便说一句,sizeof(read_from_server)只有read_from_server在数组类型时才能正常工作。如果它是指向缓冲区的指针,那么它也被破坏了。

于 2013-08-22T14:18:21.143 回答