Piping Processes (a little different)
Hello,
Is there a way to pipe data between 2 processes in C++? Now, this isn't normal pipe that I am asking for. This is best explained with what I need it for. I'd like to write an application that allows the user to edit their file in a external editor (say, pico or nano), and then when they save the file and exit, it is really saved back to the pipe and when pico exits, they are returned to my application. Is there really any way to do this? I assume it isn't built into the standard C++ functions or the STL, but perhaps there is a library out there than can do this? TIA. |
The usual way to invoke an external editor is to have it write to a temporary file in /tmp. E-mail clients like pine and mutt do this.
However, you actually can use pipes in C/C++ programs. See "man pipe" or "man 2 pipe" for the low-level system call. For higher level pipe functions, see "man popen" and "man pclose". There's a book from Wrox Press Inc. called "Beginning Linux Programming". The source code of the examples in the book (GPL'd) can be downloaded from: http://www.wrox.com/dynamic/books/do...sbn=1874416680 See chapter 11 for pipe examples. |
A few questions. First off, do I have to include any special libraries to access the commands like pipe() and such? I might not want to deal with the complexities of pipes, so what command would I use to run pico? Is there a command like exec() that I can use, or do I have to call a command with 'sh'?
|
Here's an C program that lets the user edit a file with pico. It should work in C++, but you may want to change the code to be more C++ specific
For simplicity it lacks all error-checking. Especially it will segfault if the user does not write the file from the editor (pico). Code:
#include <stdio.h> |
Wow, excellent! Thank you very much for the sample code, that really cleared things up for me. Finally, I have a starting point to work with on this project! :)
|
All times are GMT -5. The time now is 05:54 PM. |