5 replies
by nikki96
Use popen instead. It will do the redirect for you.
Excerpt from man page:
NAME
popen, pclose - initiate pipe to/from a process
SYNOPSIS
#include
FILE *popen(const char *command, const char *type);
int pclose (FILE *stream);
DESCRIPTION
The popen() function creates a pipe between the calling pro-
gram and the command to be executed. The arguments to
popen() are pointers to null-terminated strings. command
consists of a shell command line. type is an I/O mode,
either r for reading or w for writing. The value returned
is a stream pointer such that one can write to the standard
input of the command, if the I/O mode is w, by writing to
the file stream (see intro(3)); and one can read from the
standard output of the command, if the I/O mode is r, by
reading from the file stream. Because open files are
shared, a type r command may be used as an input filter and
a type w as an output filter.
#include
#include
main()
{ char *cmd = "/usr/bin/ls *.c";
char buf[BUFSIZ];
FILE *ptr;
if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, BUFSIZ, ptr) != NULL) (void) printf("%s", buf);
return 0;
}
This program will print on the standard output (see
stdio(3S)) all the file names in the current directory that
have a .c suffix.
Hope this helps.
Nikki
by tech
Another technique is to dup() the stdin and stdout descriptors, then fork() another process. The duped descriptors can now be used for bi-directional communications between the processes. The parent process can send data to the child process through the dup'ed stdout descriptor, which is received by the child process through the duped stdin descriptor. The child process can send data to the parent through its duped stdout descriptor, which the parent receives in the duped stdin descriptor.
I know it's a little confusing, but using this technique properly allows you to chain a series of processes together via stin/stdout.
by tech
by tech
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending