用分隔符'#'拆分char数组并保存在另一个数组中

时间:2013-08-22 13:23:48

标签: c linux

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

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= ......糟糕的结果为什么?

1 个答案:

答案 0 :(得分: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);

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