キーボードからコマンドを読み取って書き込むプログラムを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;
}
}
これは正常ですか、それとも何か間違ったことをしている可能性がありますか?