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.


|