Redirection is accomplished by the shell, not by the execution or piping mechanism. Since popen gives you a FILE* object on which you can read from the program's standard output, you should simply read from the result file and write that result to a FILE* that you construct with fopen("test.txt","w"). Also, you should be aware that the IEEE Std. 1003.1 "POSIX" function is popen, the _popen business is a MSFT thing.
A:
Michael Aaron Safyan
2010-04-19 00:50:27
A:
Thanks Aaron. Yep, I'm currently on Ms Visual Studio right now. My other way is, how can I concatenate these pointers to become a full statement that can be send inside the _popen? Here is my plan :
char *cmd1 = "java -jar c:\simmetrics.jar";
char *cmd2 = "some text";
char *cmd3 = "some other text";
char *cmd4 = "> c:\test.txt";
char *final = cmd1 + cmd2 + cmd3 + cmd4; // <------ how can I achieve this?
FILE *child = _popen(final, "r"); <--- 'final' contains complete statement from above
karikari
2010-04-19 01:09:05
As I already stated, you don't want to merge cmd4 in... you have to parse it and realize that you need to redirect to a file. As for merging cmd1...cmd3, you can use std::ostringstream or std::string if you are using C++. Otherwise, you can use snprintf or strncat, although frankly I would just compute the total size buffer needed, allocate such a buffer, and then copy the commands in order into the buffer without relying on any of the existing C functions.
Michael Aaron Safyan
2010-04-19 01:41:59