H
Hlk.Turk
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
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