how to simulate keyboard input for a program in C

c, iostream, ipc, stdio

Solution

It's not a program you're looking for, it's capabilities of your shell:

 program < input.txt

This way you push input.txt as stdin to program.

If you want a program, then you can just run a program and reroute it's output to the other programs input.

Problem

I have a binary of a program that waits for an input using scanf. I need to write a C code that will be able to simulate keyboard input. i.e. close the stdin pointer for that binary and instead repoint it to a file. I used this code ``` int main() { FILE *fin; int result; char string[80]; close(0); fin = fopen("text", "r"); if(NULL == fin) { printf("Unable to open file."); return 0; } dup(fin); return 0; } ``` But i found that each program has its own stdin pointer. Is there a way for me to simulate keyboard input for one binary from another C program ?

Original source