垃圾退格命令行

时间:2013-11-02 10:24:26

标签: c

我在C中制作了一个程序,它从键盘读取命令并写入它们,它的工作原理如下:

put this is stackoverflow
  • 操作码: put
  • 数据:这是
  • key: stackoverflow

有时当我在编写命令并执行退格时,它会留下垃圾:

数据:a`

代码非常复杂,但这里有一些部分:

    printf("data: %s\n",(char*)msg->content.value->data);//data is *void

    ...
    char command[MAX_MSG];
    fgets(command, sizeof(command), stdin);

    command[strcspn (command, "\n")] = '\0';
    aux_command_key = strdup(command);
    aux_command_data = strdup(aux_command_key);

    if(strcmp(opcode, "put") == 0){
           sendMessage->opcode = OC_PUT;
           sendMessage->c_type = CT_ENTRY;
           char *key = getKey(aux_command_key, opcode);

           if(key == NULL){
                     printf("Invalid number of arguments.\n");
                     success = -1;
           }

           else{
                     key = strdup(key);
                     char *data = strdup(getData(aux_command_data, opcode, key));                   sendMessage->content.entry = entry_create(key, data_create2(strlen(data), data));
           }
     }
     ...
     else if(strcmp(opcode, "get") == 0){
           sendMessage->opcode = OC_GET;            
           sendMessage->c_type = CT_KEY;
           char *key = getKey(aux_command_key, opcode);

           if(key == NULL){
                     printf("Invalid number of arguments.\n");
                     success = -1;
           }

           else{
                     key = strdup(key);
                     sendMessage->content.key = key;
           }
     }

这是正常的还是我可能做错了什么?

0 个答案:

没有答案