C 垃圾退格命令行

C 垃圾退格命令行,c,C,我用C语言编写了一个程序,从键盘上读取命令并写入,其工作原理如下: put this is stackoverflow 操作码:put 数据:这是 关键字:堆栈溢出 有时,当我编写命令并使用退格时,它会留下垃圾: 数据:a`� 代码非常复杂,但以下是其中的一些部分: printf("data: %s\n",(char*)msg->content.value->data);//data is *void ... char command[MAX_MSG]

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

put this is stackoverflow
  • 操作码:put
  • 数据:这是
  • 关键字:堆栈溢出
有时,当我编写命令并使用退格时,它会留下垃圾:

数据: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;
           }
     }

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

向我们展示您正在读取用户输入的代码,它非常复杂。但我放了一些,肯定不正常,需要检查。有没有关于-Wall的编译器警告?如果您提供的输入代码处理失败,则可能会有所帮助。任何更详细的信息。听起来更像是终端(tty)设置以及如何配置退格键与之交互的问题。您是否尝试了Ctrl-H而不是backspace?还是移动退格?如果这两种方法都起作用,则可能是tty配置问题。
stdin
通常是缓冲的。因此,在
fgets()
的缓冲区中不应出现退格。相反,backspace在控制台中起作用,只是将输入缩短1。操作生成
stdin
无缓冲或使用管道输入。