ckeystroke

Running a program and recording keystrokes in C


I wanted to run a binary code inside my C program (which is an interactive code) and record each keystrokes and list it in an output file. I have written the following, but seems like it is not working.

#include <stdio.h>

#define MAX_LIMIT 200

int main() {
    while (system("my_binary")) { 
        FILE *out_file = fopen("name_of_file", "w");
        char str[MAX_LIMIT];
        fgets(str, MAX_LIMIT, stdin);
        fprintf(out_file, "%s\n", str); 
        fclose(out_file);
    }
}

any suggestions what went wrong here!


Solution

  • Writing a program to do this is tricky and system specific. Yet if your system has a tee utility, you can run this command from the shell instead:

    tee name_of_file | ./my_binary