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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-23-2002, 11:42 AM
|
#1
|
|
LQ Newbie
Registered: Nov 2002
Location: Ohio
Distribution: Mandrake 8.2/PPC
Posts: 19
Rep:
|
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.
|
|
|
|
12-23-2002, 03:46 PM
|
#2
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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.
|
|
|
|
12-24-2002, 10:42 AM
|
#3
|
|
LQ Newbie
Registered: Nov 2002
Location: Ohio
Distribution: Mandrake 8.2/PPC
Posts: 19
Original Poster
Rep:
|
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'?
|
|
|
|
12-25-2002, 05:54 PM
|
#4
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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.
|
|
|
|
12-27-2002, 08:17 AM
|
#5
|
|
LQ Newbie
Registered: Nov 2002
Location: Ohio
Distribution: Mandrake 8.2/PPC
Posts: 19
Original Poster
Rep:
|
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! 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:19 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|