General discussion

Locked

redirect stdin and stdout in c

By tech ·
How do I redirect the stdin and stdout in such a way that it could be put into a string? I want to do this so that a program run using system() or one of the exec functions would recieve its input from the program and its output would go to the program, instead of to the screen. This is in the c programming language in Linux.

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

redirect stdin and stdout in c

by nikki96 In reply to redirect stdin and stdout ...

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 <stdio.h>

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

Collapse -

redirect stdin and stdout in c

by tech In reply to redirect stdin and stdout ...

Just what I was looking for. I had not thought to use popen. Thank you.

Collapse -

redirect stdin and stdout in c

by Consultant DaveA In reply to redirect stdin and stdout ...

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.

Collapse -

redirect stdin and stdout in c

by tech In reply to redirect stdin and stdout ...

This information was somewhat helpfull, but not really specific enough regarding how one would go about using the described commands. Thank you anyway.

Collapse -

redirect stdin and stdout in c

by tech In reply to redirect stdin and stdout ...

This question was closed by the author

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums