我在C中制作了一个程序,它从键盘读取命令并写入它们,它的工作原理如下:
put this is 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;
}
}
这是正常的还是我可能做错了什么?