ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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:
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>
#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;
}
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.