exec and buffered output

This is a discussion on exec and buffered output within the C Programming forums, part of the General Programming Boards category; Hello everyone, I need some help please. Any appreciated. I don't know if I am having a memory lapse or ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    9

    Question 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

  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131

    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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    What about popen()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    9

    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

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    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

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21