M Azeem N
M Azeem N

Reputation: 923

How to write some command on a batch file through C++ program?

I need some help regarding my C++ program. A batch file named abc.bat is located somewhere in my hardisk. I know that in C++ I can use this line of code to execute that abc.bat file:

system ("file path here\\abc.bat");

I want to send some commands to that batch file so that after executing that abc.bat file my C++ program should write commands to its console and execute them. How can I do that?

Upvotes: 3

Views: 3466

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993343

You can do this by opening a pipe. In brief:

FILE *bat = popen("path\\abc.bat", "w");
fprintf(bat, "first command\n");
fprintf(bat, "second command\n");
pclose(bat);

The text you write to bat will end up on the standard input of the batch file.

Upvotes: 3

Related Questions