Re: C++: Redirecting stdin & stdout to same file with freopen

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance





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



Relevant Pages

  • Re: C++: Redirecting stdin & stdout to same file with freopen
    ... both stdin & stdout to a common file at the same time. ... use cin to read from that file and cout to write to it. ...
    (microsoft.public.vc.language)
  • cin is eating my output
    ... I have found to get the first line to appear is to put a newline at ... Despite the exception cin is still waiting for its input. ... I pass some text to cout for output. ...
    (comp.lang.cpp)
  • Re: Using Module on Module
    ... module UnidadeAritmetica(a, b, s, cIn, cOut, f); ... Aritmetica function so it only return the operators needed in ...
    (comp.lang.verilog)
  • Re: Currant status of iostream.h
    ... Tutorials etc. always taught ... There is no cin, cout, iostream.h, etc. in C. ... Get the 2nd edition of the C language book, ...
    (comp.lang.c)
  • Mp3 decoder library or player for console
    ... and it accepts controls from stdin. ... int main ... cin>> a; ... cout << "Keine DLL!"; ...
    (comp.programming)