LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-23-2002, 11:42 AM   #1
mackie_lin
LQ Newbie
 
Registered: Nov 2002
Location: Ohio
Distribution: Mandrake 8.2/PPC
Posts: 19

Rep: Reputation: 0
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.
 
Old 12-23-2002, 03:46 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.
 
Old 12-24-2002, 10:42 AM   #3
mackie_lin
LQ Newbie
 
Registered: Nov 2002
Location: Ohio
Distribution: Mandrake 8.2/PPC
Posts: 19

Original Poster
Rep: Reputation: 0
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'?
 
Old 12-25-2002, 05:54 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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;
}

Last edited by Hko; 12-25-2002 at 05:59 PM.
 
Old 12-27-2002, 08:17 AM   #5
mackie_lin
LQ Newbie
 
Registered: Nov 2002
Location: Ohio
Distribution: Mandrake 8.2/PPC
Posts: 19

Original Poster
Rep: Reputation: 0
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with piping mijohnst Linux - General 7 10-21-2005 04:14 PM
Piping keyboard input to two processes (or threads) caffeinebot Programming 3 05-17-2005 04:51 PM
Unzip + Piping iago Linux - Software 2 11-17-2004 02:11 PM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
Piping question OtisLinux Linux - Software 1 02-12-2004 01:38 PM


All times are GMT -5. The time now is 09:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration