LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Piping Processes (a little different) (https://www.linuxquestions.org/questions/programming-9/piping-processes-a-little-different-39205/)

mackie_lin 12-23-2002 11:42 AM

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.

Hko 12-23-2002 03:46 PM

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.

mackie_lin 12-24-2002 10:42 AM

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'?

Hko 12-25-2002 05:54 PM

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>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int  c;
    char filename[40];
    char cmdline[60];
    FILE *tmp;

    /* Create a unique temp filename. */
    /* Note: the tmpnam() function is deprecated */
    /*      but is the most easy to use in this case */
    tmpnam(filename);

    /* Write commandline to the string 'cmdline' . */
    sprintf(cmdline, "pico %s", filename);

    /* Start editor and wait for it to finish. */
    system(cmdline);

    /* Open temp file */
    tmp = fopen(filename, "r");

    /* Do something with the text entered into the editor.
    /* Here it prints the file contents with spaces */
    /* replaced by underscores */
    while ((c=fgetc(tmp))!=EOF) putchar(c==' '?'_':c);

    /* Close and delete temp file. */
    fclose(tmp);
    unlink(filename);
    return 0;
}


mackie_lin 12-27-2002 08:17 AM

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.