how to simulate keyboard input for a program in C

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 ?

Answers


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.


Need Your Help

Authentication Form in ASP.Net

asp.net vb.net authentication

I am planning to create Login Form for my system. Is it better to use ASP.NET built in Authentication and role management OR create my own way? Which is better and convenient? I want the administra...