Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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?

like image 976
M Azeem N Avatar asked Feb 03 '26 21:02

M Azeem N


1 Answers

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.

like image 122
Greg Hewgill Avatar answered Feb 06 '26 13:02

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!