Executable call into an executable: can I pass streams to it?

Pages: 12
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...

Thanks in advance
closed account (S6k9GNh0)
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.
Last edited on
Ok, so what I should use?

The analogue command in Boost libraries, maybe?
Ok, so what I should use?


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().
Last edited on
Ok, let's assume I find an alternative...but what about the streaming stuff?
Can anyone give me an example?
I have to call a C executable from a C++ executable
¿Do you have the code? ¿Why don't just make it a function?

but what about the streaming stuff?
Just know about popen or pipe. However they work with FILE* or file descriptors.
closed account (S6k9GNh0)
If you're using Windows, then look into the CreateProcess function...
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.
If you're using Windows, then you would pass the argument to the new process in the same string that you use to call the new program. For example:
 
CreateProcess(NULL, "notepad.exe C:\\a.txt", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);

Just like a command line. The variable "si" is a STARTUPINFO type and "pi" is a PROCESS_INFORMATION type.

EDIT: Sorry, I forgot this: http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx

EDIT2: I forgot a slash in the command.
Last edited on
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.
Did you try it like in your original post? (Only make sure there is a space between the command name and the argument...)
If you can pass a number as a parameter to the other program, then you can just as easily pass a pointer to a stream to the other program. ;)
Here you are my little example.
hello.cpp (I cannot modify this!):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
using namespace std;

int main( int argc, char *argv[] )
{
	string line;
	ifstream f( argv[1] );

	while(!f.eof())
	{
		getline(f, line);
		cout << line << endl;
	}

	return 0;
}


Run example:
1
2
3
4
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>

using namespace 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?

I would like something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main ()
{
	int ret;
	ifstream inputFile;

	inputFile << "1 - This is" << endl;
	inputFile << "2 - a test"   << endl;
	inputFile << "3 - file."  << endl;

	ret = execl ("./hello", "./hello", inputFile, (char *)0);

	return 0;
}


This last example, of course, doesn't work...how can I modify it in order to make it work?
Remember that I cannot modify hello.cpp...
No one can help me please? :-(
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.
It looks to me like it's impossible if he can't modify the other program...?
@ 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.
They're just trying to spoof where the output is coming from right? I didn't go into detail because I don't even know if the OP is on Windows.

- GetStartupInfo() :http://msdn.microsoft.com/en-us/library/ms683230(VS.85).aspx

Gives you the output handle for the new process then

- WriteConsole(): http://msdn.microsoft.com/en-us/library/ms687401(VS.85).aspx

To finish up. Or am I missing something? I haven't tried it so it may be a bit more involved but it should definatly be possible.
No, that's not what the OP wants:
Is it possible to do that without writing these files on the filesystem, but using file streams?
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?

He wants the other program to load stuff from an existing file stream instead of having to use an existing file.
Last edited on
Pages: 12