问题
Say i have an exe, lets say sum.exe. Now say the code for sum.exe is
void main ()
{
int a,b;
scanf ("%d%d", &a, &b);
printf ("%d", a+b);
}
I wanted to know how i could run this program from another c/c++ program and pass input via stdin like they do in online compiler sites like ideone where i type the code in and provide the stdin data in a textbox and that data is accepted by the program using scanf or cin. Also, i wanted to know if there was any way to read the output of this program from the original program that started it.
回答1:
How to do so is platform dependent.
Under windows, Use CreatePipe and CreateProcess. You can find example from MSDN : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
Under Linux/Unix, you can use dup()
/ dup2()
One simple way to do so is to use a Terminal (like command prompt in windows) and use | to redirect input/output.
Example:
program1 | program2
This will redirect program1's output to program2's input. To retrieve/input date, you can use temporary files, If you don't want to use temporary files, you will have to use pipe.
For Windows, (use command prompt):
program1 <input >output
For Linux, you can use tee
utility, you can find detail instruction by typing man tee
in linux terminal
回答2:
The easiest way I know for doing this is by using the popen()
function. It works in Windows and UNIX. On the other way, popen()
only allows unidirectional communication.
For example, to pass information to sum.exe
(although you won't be able to read back the result), you can do this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
f = popen ("sum.exe", "w");
if (!f)
{
perror ("popen");
exit(1);
}
printf ("Sending 3 and 4 to sum.exe...\n");
fprintf (f, "%d\n%d\n", 3, 4);
pclose (f);
return 0;
}
回答3:
In C on platforms whose name end with X (i.e. not Windows), the key components are:
pipe
- Returns a pair of file descriptors, so that what's written to one can be read from the other.fork
- Forks the process to two, both keep running the same code.dup2
- Renumbers file descriptors. With this, you can take one end of a pipe and turn it into stdin or stdout.exec
- Stop running the current program, start running another, in the same process.
Combine them all, and you can get what you asked for.
回答4:
This is my solution and it worked:
sum.cpp
#include "stdio.h"
int main (){
int a,b;
scanf ("%d%d", &a, &b);
printf ("%d", a+b);
return 0;
}
test.cpp
#include <stdio.h>
#include <stdlib.h>
int main(){
system("./sum.exe < data.txt");
return 0;
}
data.txt
3 4
Try this solution :)
回答5:
It sounds like you're coming from a Windows environment, so this might not be the answer you are looking for, but from the command line you can use the pipe redirection operator '|' to redirect the stdout of one program to the stdin of another. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true
You're probably better off working in a bash shell, which you can get on Windows with cygwin http://cygwin.com/
Also, your example looks like a mix of C++ and C, and the declaration of main isn't exactly an accepted standard for either.
来源:https://stackoverflow.com/questions/21231188/how-do-i-run-a-program-from-another-program-and-pass-data-to-it-via-stdin-in-c-o