freopen

Discussion in 'C Programming' started by Hlk.Turk@gmail.com, Aug 18, 2007.

  1. Guest

    Hi All,

    I have a question regarding the standard C library.

    Is it possible to have one process (program) that
    redirects its stdin to a file, and have a second program
    use that file write/append to that file. The first program
    will then read its redirected standard input file and
    process that...

    The first program is like this:

    #include <stdio.h>
    #include <string.h>

    void main (void)
    {
    FILE *f_stdin = NULL;
    char buf [501];

    f_stdin = freopen ("stdin", "a+", stdin);
    if (f_stdin != NULL)
    {
    buf[0] = '\0';
    while (1)
    {
    fgets (buf, 500, f_stdin);
    if (strlen (buf) > 0)
    printf ("buf: %s\n", buf);
    if (strcmp (buf, "q") == 0)
    break;
    buf[0] = '\0';
    }
    }
    else
    {
    perror ("error(first prog)");
    }
    }

    The second program is:

    #include <stdio.h>

    void main (void)
    {
    FILE *f_stdin = NULL;

    f_stdin = fopen ("stdin", "a+");
    if (f_stdin != NULL)
    {
    fputs ("a test", f_stdin);
    }
    else
    {
    perror ("error(second prog)");
    }
    }

    The code below (creating two executables) compiles.
    I can redirect stdin in the first program and can open
    and write in the second program. Altough the string is
    written to the file, I can not read that from the first
    program.

    Can you see anything wrong in the above code? Is the
    freopen usage correct? Can freopen be used to
    redirect std streams to files and have other processes
    write to these files and command the first proces?

    Regards,
    ht
    , Aug 18, 2007
    #1
    1. Advertising

  2. writes:

    > Hi All,
    >
    > I have a question regarding the standard C library.
    >
    > Is it possible to have one process (program) that
    > redirects its stdin to a file, and have a second program
    > use that file write/append to that file. The first program
    > will then read its redirected standard input file and
    > process that...


    With a following wind and lot fiddling you can get this sort of thing
    to work on some platforms. However, what you are doing -- a pair of
    programs connected by a buffer -- is so common that there are loads of
    way to do it properly. The best way to do it will depend on how
    portable you want your solution to be.

    Ultimately you will have to go and ask in a group that deals with the
    kind of systems that you want this to work on. Standard C (the topic
    here) can do little more than read and write streams.

    <code snipped>

    > Can you see anything wrong in the above code? Is the
    > freopen usage correct? Can freopen be used to
    > redirect std streams to files and have other processes
    > write to these files and command the first proces?


    The freopen is a red herring. The program could just as well use
    fopen. On some system, you don't need a file at all -- the output of
    one can be "plugged" into the input of the other.

    --
    Ben.
    Ben Bacarisse, Aug 18, 2007
    #2
    1. Advertising

  3. SM Ryan Guest

    wrote:

    # Is it possible to have one process (program) that
    # redirects its stdin to a file, and have a second program
    # use that file write/append to that file. The first program
    # will then read its redirected standard input file and
    # process that...

    As a counterexample, it will probably not work the way you expect
    on Unix. If you open read a plain disk file, the kernel does not
    change the behavior if there are writers - either you get data
    that is currently in the file, or you get an EOF. You don't get
    paused while other writers catch up. (There are other types of
    files that this will work on in Unix, but these types of files
    are not available on all other systems.)

    Perhaps your stdio will let read past an EOF so you can keep
    trying to see if the file has been extended. (The underlying
    Unix I/O permits this, allowing commands like tail -f.) But I
    think this is system dependent behaviour.

    On Unix freopn to stderr is irrelevant to this: it's a property
    of the files themselves not the FILE* connected to them.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Raining down sulphur is like an endurance trial, man. Genocide is the
    most exhausting activity one can engage in. Next to soccer.
    SM Ryan, Aug 19, 2007
    #3
  4. Kevin Handy Guest

    wrote:
    > Hi All,
    >
    > I have a question regarding the standard C library.
    >
    > Is it possible to have one process (program) that
    > redirects its stdin to a file, and have a second program
    > use that file write/append to that file. The first program
    > will then read its redirected standard input file and
    > process that...


    Success in this manner is very dependent on which
    OS you are using. You should direct this question
    to the proper developer newsgroup for that OS.

    Under the various *nix OS's, you may have a 'mkfifo'
    or similar command (try 'man mkfifo'). This is almost
    exactly what you want.

    However, under *nix, you usually get this behavior
    simply by doing 'prog1 | prog2' at the command line.
    No programming required. The MS-DOS command shell
    does a half-hearted emulation of this.

    Under other OS's, you may or may not have similar
    functionality, just under a different name.

    ....

    > The code below (creating two executables) compiles.
    > I can redirect stdin in the first program and can open
    > and write in the second program. Altough the string is
    > written to the file, I can not read that from the first
    > program.
    >
    > Can you see anything wrong in the above code? Is the
    > freopen usage correct? Can freopen be used to
    > redirect std streams to files and have other processes
    > write to these files and command the first proces?
    >
    > Regards,
    > ht
    >


    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Kevin Handy, Aug 20, 2007
    #4
  5. Guest

    On 18 A ustos, 21:05, wrote:
    > Hi All,
    >
    > I have a question regarding the standard C library.
    >
    > Is it possible to have one process (program) that
    > redirects its stdin to a file, and have a second program
    > use that file write/append to that file. The first program
    > will then read its redirected standard input file and
    > process that...
    >
    > The first program is like this:
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > void main (void)
    > {
    > FILE *f_stdin = NULL;
    > char buf [501];
    >
    > f_stdin =freopen("stdin", "a+", stdin);
    > if (f_stdin != NULL)
    > {
    > buf[0] = '\0';
    > while (1)
    > {
    > fgets (buf, 500, f_stdin);
    > if (strlen (buf) > 0)
    > printf ("buf: %s\n", buf);
    > if (strcmp (buf, "q") == 0)
    > break;
    > buf[0] = '\0';
    > }
    > }
    > else
    > {
    > perror ("error(first prog)");
    > }
    >
    > }
    >
    > The second program is:
    >
    > #include <stdio.h>
    >
    > void main (void)
    > {
    > FILE *f_stdin = NULL;
    >
    > f_stdin = fopen ("stdin", "a+");
    > if (f_stdin != NULL)
    > {
    > fputs ("a test", f_stdin);
    > }
    > else
    > {
    > perror ("error(second prog)");
    > }
    >
    > }
    >
    > The code below (creating two executables) compiles.
    > I can redirect stdin in the first program and can open
    > and write in the second program. Altough the string is
    > written to the file, I can not read that from the first
    > program.
    >
    > Can you see anything wrong in the above code? Is thefreopenusage correct? Canfreopenbe used to
    > redirect std streams to files and have other processes
    > write to these files and command the first proces?
    >
    > Regards,
    > ht


    Thank you all for the responses.

    ht
    , Aug 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur

    freopen & cout

    Alex Vinokur, Dec 2, 2003, in forum: C++
    Replies:
    1
    Views:
    1,182
    Jack Klein
    Dec 3, 2003
  2. Alex Vinokur

    freopen, fclose and stdout

    Alex Vinokur, Dec 3, 2003, in forum: C Programming
    Replies:
    4
    Views:
    8,317
    vidyaani
    Jan 11, 2013
  3. Teh Charleh

    freopen()

    Teh Charleh, Dec 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    475
    Enrico `Trippo' Porreca
    Dec 7, 2003
  4. John Devereux

    freopen()

    John Devereux, Sep 27, 2005, in forum: C Programming
    Replies:
    3
    Views:
    493
    John Devereux
    Sep 27, 2005
  5. tmpfile and freopen

    , Jun 5, 2006, in forum: C Programming
    Replies:
    1
    Views:
    352
    Barry Schwarz
    Jun 15, 2006
Loading...

Share This Page