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 > Re: freopen usa...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 6 Topic 991 of 1183
Post > Topic >>

Re: freopen usage

by Jack Klein <jackklein@[EMAIL PROTECTED] > Sep 4, 2007 at 05:10 PM

On 21 Aug 2007 21:29:19 GMT, Hlk.Turk@[EMAIL PROTECTED]
 wrote in
comp.lang.c.moderated:

> Hi All,
> 
> I have a question regarding the standard C library.

Actually, you don't, you have a question regarding the behavior of
your platform.

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

You most certainly have a program redirect its stdin to come from a
file, if that file has already been successfully created, written, and
closed by another program.

The C standard does not define or even recognize the concept of a
"process", multiple threads of execution, or more than one C program
executing at one time.

> The first program is like this:
> 
> #include <stdio.h>
> #include <string.h>
> 
> void main (void)

main() returns an int.  Always.  So your program has undefined
behavior.

> {
>    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);

Presumably, if the fgets() call hits end of file on the first call, it
will set the EOF indicator for the stream and no further read
operations will be successful unless you do something to reset EOF
first.  Consult your C reference for the use of the standard library
clearerr() function.

>            if (strlen (buf) > 0)
>                printf ("buf: %s\n", buf);
>            if (strcmp (buf, "q") == 0)
>                break;
>            buf[0] = '\0';
>        }
>    }
>    else
>    {
>        perror ("error(first prog)");

freopen() is not obliged to set errno on failure, so there is no
guarantee that the output of perror() will be meaningful.

>    }
> }
> 
> 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);

Failing to terminate the last line of a text file with a newline has
implementation-defined consequences, including the possibility that
the last line, which is the only line in this case, may not actually
be written to the file.

>    }
>    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?

Again, the answer is that C does not specify an answer to your
question, as it has no concept of or sup****t for "other processes".

You need to ask this question on a group that sup****ts your specific
compiler/OS combination, as this has a lot to do with your operating
system.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.para****ft.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
-- 
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 Fri Nov 21 12:46:30 CST 2008.