Re: C++: Redirecting stdin & stdout to same file with freopen
- From: Ray Mitchell <RayMitchell_NOSPAM_@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 20 Sep 2007 09:54:00 -0700
"Ulrich Eckhardt" wrote:
Ray Mitchell wrote:
I have an application where it would be convenient if I could redirect
both stdin & stdout to a common file at the same time. Then I could
use cin to read from that file and cout to write to it.
This seems very strange, your output will probably overwrite your input. Is
that really what you want?
Anyway, here you go:
fstream inout("whatever.text");
cin.rdbuf(inout.rdbuf());
cout.rdbuf(inout.rdbuf());
Alternatively, and that is what I often end up doing, is that I don't use
cout/cin directly but instead pass a reference to a stream(s) to the
functions.
Uli
Ulrich,
This seems very strange, your output will probably overwrite your input. Is
that really what you want?
Yes, it is admittedly a strange request, but I don't see your point
regarding overwriting. Like any r/w file, you simply need to know where you
are in the file when you perform an I/O operation. My uncertainty concerns
the concept of redirecting a 2nd stream into a file that is already open.
Maybe it's not an issue, but I don't understand it with enough depth to be
comfortable with it. I did it by doing the following (with appropriate
failure tests, or course) and it seemed to work fine on WinXP:
freopen("FileName", "r", stdin);
freopen("FileName", "w", stdout);
cin << x;
....reposition...
cout >> y;
etc.
The reason I need to do it is because I teach a C/C++ Programming 1 course
wherein I require the students to write various programs in which they use
the cin/cout or scanf/printf familes (depending upon which language I
request). This enables them to get some of their input from the keyboard and
print some of their output to the screen. However, I also want to be able to
test their code myself by supplying my own input and analyzing the output.
So, I essentially need to be able to capture and parse their screen output as
well as take over their keyboard. Since it is a beginning class I don't want
to complicate matters (at first at least) by introducing file I/O, thereby
forcing them to deal with the extra complication of opening files directly.
It, thus, seemed like the easiest way to do this without impacting them was
to simply have my test code redirect stdin/stdout to a common file. Then
they can run their tests first, then comment in and run my tests. However,
since they use a wide variety of compilers and operating systems I want to be
sure that what I am doing is not working only because of some quirk rather
than because of being totally portable.
Thanks,
Ray
.
- Follow-Ups:
- Re: C++: Redirecting stdin & stdout to same file with freopen
- From: Brian Muth
- Re: C++: Redirecting stdin & stdout to same file with freopen
- References:
- Re: C++: Redirecting stdin & stdout to same file with freopen
- From: Ulrich Eckhardt
- Re: C++: Redirecting stdin & stdout to same file with freopen
- Prev by Date: Re: LoadLibrary error
- Next by Date: Re: Thrusting pigs ! ha ha ! this gets better every time I read it.
- Previous by thread: Re: C++: Redirecting stdin & stdout to same file with freopen
- Next by thread: Re: C++: Redirecting stdin & stdout to same file with freopen
- Index(es):
Relevant Pages
|