Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > freopen usage
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 991 of 1161
Post > Topic >>

freopen usage

by Hlk.Turk@[EMAIL PROTECTED] Aug 21, 2007 at 09:29 PM

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
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 6 Posts in Topic:
freopen usage
Hlk.Turk@[EMAIL PROTECTED  2007-08-21 21:29:19 
Re: freopen usage
=?ISO-8859-9?Q?Hans-Bernh  2007-09-04 17:09:20 
Re: freopen usage
Jonathan Leffler <jlef  2007-09-04 17:09:52 
Re: freopen usage
Jack Klein <jackklein@  2007-09-04 17:10:24 
Re: freopen usage
"Douglas A. Gwyn&quo  2007-09-04 17:11:56 
Re: freopen usage
ta0kira@[EMAIL PROTECTED]  2007-09-04 17:13:49 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 8:32:32 CDT 2008.