473,544 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

freopen


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(firs t 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(seco nd 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 '07 #1
4 4139
Hl******@gmail. com 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.
Aug 18 '07 #2
Hl******@gmail. com 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.
Aug 19 '07 #3
Hl******@gmail. com 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 =----
Aug 20 '07 #4
On 18 A ustos, 21:05, Hlk.T...@gmail. com 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(firs t 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(seco nd 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 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3159
by: Alex Vinokur | last post by:
Hi, Where has output-to-cout gone in the program below? Thanks, ========= C++ code : File foo.cpp : BEGIN ========= #include <cstdio> #include <cassert> #include <iostream>
4
25638
by: Rouben Rostamian | last post by:
Consider the following demo program: #include <stdio.h> int main(void) { unsigned char c; for (c=0; c<15; c++) putchar(c);
2
12587
by: Alex Vinokur | last post by:
Hi, freopen closes stdout, but it seems that fclose doesn't restore it (see sample below). How can one print to stdout after using freopen? ========= C code : File foo.c : BEGIN ========= #include <stdio.h>
8
6065
by: John Forkosh | last post by:
I have a C program that writes binary output to stdout, which works fine in Unix/Linux. But when someone else compiled and ran it in Windows, every time the program emitted a 0x0A, Windows interpreted it as an lf, and preceded it with a spurious 0x0D cr. Cute. Is there some way I can freopen() (so to speak) stdout in binary mode under...
3
561
by: John Devereux | last post by:
Hi, I am a bit confused about the behaviour of freopen() and the standard i/o streams. I am trying to redirect a stream (stderr) to a particular "file". But, if the call fails I want it to stay as it was, or at least be able to redirect it somewhere else (e.g. stdout). For example I would like to do,
1
2408
by: joshuawilsonster | last post by:
I have checked: http://c-faq.com/stdio/undofreopen.html and don't believe it addresses my question. I am dealing with large files of numbers and want to take in small chunks and act on them--sometimes using information based on the whole file. My approach is to read in the whole file in chunks, place it in tempfile = tmpfile(); and then run...
1
2127
by: system55 | last post by:
is it possible to reopen a file (using freopen) eventhough that file is already opened by an fopen function???
2
8452
by: Guillaume Dargaud | last post by:
Hello all, a while ago I was pointed towards freopen as a way to redirect stderr to a log file. It works great, but apparently the app also writes a few lines to stdout. Now I could redirect to 2 separate files, but would rather keep the 2 flows together. Is it correct to do this: stderr=freopen(LogFile, "w", stderr);...
5
8947
by: Joakim Hove | last post by:
Hello, I have written a program in C; this programs uses an external proprietary library. When calling a certain function in the external library, the particular function writes a message to stdout. I am not particularly interested in this message, and would like to silence it - however I do not know how to do it. (I stdout and stderr my...
0
7420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7602
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7704
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5291
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4911
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3404
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
983
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
657
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.