0

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

put this is stackoverflow
  • 操作码:
  • 数据:这是
  • 关键:堆栈溢出

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

数据:a`�</p>

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

    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;
           }
     }

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

4

0 回答 0