-
exec and buffered output
Hello everyone, :(
I need some help please. Any appreciated. I don't know if I am having a memory lapse or what, but I can not seem to remember something.
I am using an exec call to execute a command, and that works fine and dandy, but I need to direct the output of this into a buffer, instead of stdout (onto the screen). Does anyone know how to do this?
-Optimus
-
I Think
Hello,
I just read about freopen last night.
From man freopen
Code:
SYNOPSIS
#include <stdio.h>
FILE *freopen(const char *filename, const char *mode, FILE
*stream);
DESCRIPTION
The freopen() function first attempts to flush the stream
and close any file descriptor associated with stream.
Failure to flush or close the file successfully is ignored.
The error and end-of-file indicators for the stream are
cleared.
The freopen() function opens the file whose pathname is the
string pointed to by filename and associates the stream
pointed to by stream with it. The mode argument is used just
as in fopen(3C).
USAGE
The freopen() function is typically used to attach the preo-
pened streams associated with stdin, stdout and stderr to
other files. By default stderr is unbuffered, but the use of
freopen() will cause it to become buffered or line-buffered.
I think this might be what you want.
Hope it works,
Rouss
-
-
popen
The popen function looks interesting. Can it take inside it though an excecvp call? I noticed this code at one site:
#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);
(void) pclose(ptr);
return 0;
}
I am not sure if I can stick an execvp in there somewhere. This looks very promising though. Any comment apprecaited.
-optimus
-
Why would you need an exec call?
That loop executes the 'ls' command and collects its output one line at a time through calls to fgets()
It prints it, but there's nothing to stop you storing it in memory
-
I should have mentioned I am forced to use an exec call to take any command argument to run it. I need to store the output in a buffer though, and not on the screen.
V/r
optimus