Ctrl+D doesn't stop application from executing in command window

前端 未结 5 881
后悔当初
后悔当初 2020-12-07 05:21

I\'ve written a program to encrypt a given message by XOR. It works, but It doesn\'t end. Here is the code.(I have created 3 files):

encrypt.h :

void         


        
相关标签:
5条回答
  • 2020-12-07 05:30

    Just run

      $> kill -l
    

    To see the list of signals in Linux. You will not find SIGKILL (Ctrl + D) signal there :(

    Ctrl + D is SIGKILL (0) signal in Linux which is not documented anywhere. Ctrl + Z is for Windows which tell EOF and we need to press "Enter" to close.

    0 讨论(0)
  • 2020-12-07 05:41

    When I pressed Ctrl+D to stop it (in cmd)

    If that's the cmd from Windows you probably want Ctrl+Z.

    0 讨论(0)
  • 2020-12-07 05:42

    isprint() can help:

    #include <stdio.h>
    #include <ctype.h>
    
    void encrypt(char *message)
    {
        while (*message) {
            *message = *message ^ 31;
            message++;
        }
    }
    
    int main(void)
    {
        char msg[80];
    
        while (fgets(msg, 80, stdin) != NULL) {
            if (!isprint((unsigned char)*msg)) break;
            encrypt(msg);
            printf("%s", msg);
        }
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-07 05:42

    Add an exit condition:

    if( c < 0x20 ) break;
    

    You may need to add other checks also to support backspace without encoding it...

    http://www.asciitable.com/

    0 讨论(0)
  • 2020-12-07 05:43

    Ctrl-D is used for the console EOF on Unix systems.

    Ctrl-Z is used for the console EOF on Windows systems.

    0 讨论(0)
提交回复
热议问题