Hello.
I have to call a C executable from a C++ executable. Googleing a little bit I found the following:
ret = system( "./myExecutable" );
This is ok and it works, but I need to pass to myExecutable several input textual files also.
Is it possible to do that without writing these files on the filesystem, but using file streams?
Something like:
1 2 3 4
ofstream myfile;
myfile << "Textual stuff pretty long...";
string launch = "./myExecutable" + myfile; // I know it's wrong, just to give an idea...
ret = system( launch.c_str() );
I have to insert this call inside a very big loop and such a thing will save a lot of execution time...
1. Don't use system(), especially in cases like this (and since I'm not 100% sure what you're doing). http://cplusplus.com/forum/articles/11153/
2. You're OS should provide you with native tools to create and execute a process which should also provide proper ways to pass data via arguments or similar.
Could look into exec() or ShellExecute() perhaps. There aren't many ways, that I've been able to find anyway, where you can launch one program from another unless they're configured to interact with eachother with something alike CreateProcess().
I'm using Linux, but if it exist something portable, I would prefere to use it...
If you are using Linux, just read stdin. You can pipe stdout into it just like grep, etc.. You don't have to do anything fancy.
I'm not programming in bash. Give me an example on how redirect the content of a string (supposing it is the content of my file) to a call to another executable...inside a C++ source.
caneta@host ~ $ ./hello myfile.txt
1 - This is
2 - a test
3 - file.
The wrapper program which I need to modify:
exeIntoExe.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <sstream>
usingnamespace std;
int main ()
{
int ret;
ret = execl ("./hello", "./hello", "myfile.txt", (char *)0);
return 0;
}
In this way it works perfectly...but how can I modify it in order to use a stream or a string inside the wrapper program, instead of using a file name?
Now that I've seen what it is you want I can say that you are asking for something called "Interprocess Communication" and that I'm afraid is platform specific.
For Windows you'll want to look at something called a Job Space.
@ L B: In Theory he can have his application launch the other one and hook the io channels. This is done through the STARTUPINFO structure in Win32. http://msdn.microsoft.com/en-us/library/ms686331(VS.85).aspx
It has three members "HANDLE hStdInput, HANDLE hStdOutput and HANDLE hStdError" for these kinds of tasks.
IPC would make sense if hello was capable of accepting a pipe from the caller, or if it used stdin to get its input. The way it's written, there's no way for the program to do anything too sophisticated. It simply reads a command line argument and opens a file based on it. At no point does it read from stdin or perform any operations to get special parameters from its parent.