Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Q] how to redirect cin ???

2 views
Skip to first unread message

Ludo Pagie

unread,
Jun 2, 1999, 3:00:00 AM6/2/99
to
Hi all,

perhaps a straightforward thing but I couldn't figure it out;

How do you redirect stdin (cin); if your program should be able to read
input from either stdin or a file, dependent on some program argument
(which is very common on UNIX as far as I know)

thanks, Ludo
(pa...@binf.bio.uu.nl)

Lars Gullik Bjønnes

unread,
Jun 2, 1999, 3:00:00 AM6/2/99
to
Ludo Pagie <no...@nada.com> writes:

| Hi all,
|
| perhaps a straightforward thing but I couldn't figure it out;
|
| How do you redirect stdin (cin); if your program should be able to read
| input from either stdin or a file, dependent on some program argument
| (which is very common on UNIX as far as I know)

void do_all_reading(istream & is) {
...
// work on is
...
}

int main(int argc, char ** argv) {
if (argc == 1) {
do_all_reading(cin);
} else {
ifstream ifs(argv[1]);
do_all_reading(ifs);
}
}

f.ex.

Lgb

Dietmar Kuehl

unread,
Jun 2, 1999, 3:00:00 AM6/2/99
to pa...@binf.bio.uu.nl
Hi,

In article <3754F903...@nada.com>, pa...@binf.bio.uu.nl wrote:
> How do you redirect stdin (cin); if your program should be able to
> read input from either stdin or a file, dependent on some program
> argument (which is very common on UNIX as far as I know)

This question was the topic of the GotW #48 (see the discussion in
comp.lang.c++.moderated to find out more about the "Guru of the Week"
competition). The answer is basiscally, to the 'rdbuf()' to replace the
used stream buffer:

#include <fstream>
#include <iostream>
int main(int ac, char* av[])
{
std::streambuf* original = std::cin.rdbuf();
std::ifstream in;
if (ac > 1)
in.open(av[1]);
std::cin.rdbuf(in.is_open()? in.rdbuf(): original);

// do what you need to do...

std::cin.rdbuf(original);
}

It is necessary to restore the original stream buffer at the end of this
function because otherwise an illegal stream buffer might be installed
for 'cin': The stream buffer obtained from 'in' is destructed when 'in'
goes out of scope.
--
<mailto:dietma...@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

adam...@my-deja.com

unread,
Jun 2, 1999, 3:00:00 AM6/2/99
to
<snip>
>
> #include <fstream>
> #include <iostream>
<snip>

fstream includes iostream. (Not that this is of any great
consequence... :))

Seth Jones

unread,
Jun 2, 1999, 3:00:00 AM6/2/99
to
In article <3754F903...@nada.com>, no...@nada.com
says...

> Hi all,
>
> perhaps a straightforward thing but I couldn't figure it out;
>
> How do you redirect stdin (cin); if your program should be able to read
> input from either stdin or a file, dependent on some program argument
> (which is very common on UNIX as far as I know)
>

I haven't tried it myself, but I believe this will work:

filebuf fb;
cin.rdbuf( fb.open( filename, ios_base::in ) );

Seth Jones

William Roeder

unread,
Jun 2, 1999, 3:00:00 AM6/2/99
to
Ludo Pagie wrote:
>
> Hi all,
>
> perhaps a straightforward thing but I couldn't figure it out;
>
> How do you redirect stdin (cin); if your program should be able to read
> input from either stdin or a file, dependent on some program argument
> (which is very common on UNIX as far as I know)
>
> thanks, Ludo
> (pa...@binf.bio.uu.nl)
1. This is operating system dependent and not part of the standard C++
language.
2. Buy some reference books about your operating system.
3. Read the FAQs at:
http://www.eskimo.com/~scs/C-faq/top.html -- C language FAQ
http://www.cerfnet.com/~mpcline/C++-FAQs-Lite/ -- C++ language FAQ

4. Research the following newsgroups before posting:
news:comp.programming
news:comp.unix.programmer
news:comp.os.msdos.programmer
news:comp.os.ms-windows.programmer.win32
news:borland.public.cppbuilder (via forums.inprise.com newsserver)
news:borland.public.cpp.language (via forums.inprise.com newsserver)
news:microsoft.public.vcc.language (via msnews.microsoft.com newsserver)

5. On Unix, redirecting cin is performed by the shell, before the
program is even started.
--
__ _ __ If you can keep your head when others around
/ ) / / ' ) ) / you are loosing theirs, You must
/--' * / / /--' _ _ /> _ / _ __ be the one holding the axe.
/__)__/\_/\_/\_ / \_(_) (__(_/\_(/_/ (_ mailto:bro...@titan.com

Luc Bourhis

unread,
Jun 3, 1999, 3:00:00 AM6/3/99
to
Seth Jones wrote:
>
> In article <3754F903...@nada.com>, no...@nada.com
> says...
> > Hi all,
> >
> > perhaps a straightforward thing but I couldn't figure it out;
> >
> > How do you redirect stdin (cin); if your program should be able to read
> > input from either stdin or a file, dependent on some program argument
> > (which is very common on UNIX as far as I know)
> >
>
> I haven't tried it myself, but I believe this will work:
>
> filebuf fb;
> cin.rdbuf( fb.open( filename, ios_base::in ) );

Why not simply writing all the functions that are taking some inputs
like this :
void f(istream& i)
{
// Here get what you need from i
}

Then you just have to pass to these functions a object the_input
initialized as
istream& the_input = cin if you want to use standard input or
ifstream the_input(...) if you want to read data from a file
--
Luc Bourhis

Center for Particle Theory
University of Durham, UK

Dietmar Kuehl

unread,
Jun 4, 1999, 3:00:00 AM6/4/99
to
Hi,
adam...@my-deja.com wrote:
I think this is an extraction from an article I posted. Please note, that
not quoting the author of quoted text is a copyright violation unless you
are the author yourself! I don't care about the legal part but I consider
it bad style and I think it violates the Netiquette.
: <snip>

: >
: > #include <fstream>
: > #include <iostream>
: <snip>

: fstream includes iostream.

This assertion is not correct. Some implementation do in fact include
<iostream> from <fstream> but this is not required by the standard.
Actually the header <iostream> does not define anything interesting
which would justify the inclusion from <fstream>: It only defines the
standard streams 'cin', 'cout', 'cerr', and 'clog' plus their wide
character brethren. Strict reading of the standard actually does not
even require <iostream> to define anything but these 8 symbols and a
declaration of three template types ('char_traits', 'basic_istream',
and 'basic_ostream')! If there is interest in this topic, I can post
an article I posted to comp.std.c++ describing this problem.
--
<mailto:dietma...@claas-solutions.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic

Dietmar Kuehl

unread,
Jun 4, 1999, 3:00:00 AM6/4/99
to
Hi,
William Roeder (bro...@titan.com) wrote:
: 1. This is operating system dependent and not part of the standard C++
: language.

This assertion is plain false! There is a standard C++ approach to
redirect any C++ stream. Please buy and read some reference books
before you complain about articles! You might also want to research
the following newsgroups before you post unpolite articles:
- comp.lang.c++.moderated (this topic was addressed there eg. in the
thread spawned by the GotW # 48)
- comp.std.c++

William Roeder

unread,
Jun 4, 1999, 3:00:00 AM6/4/99
to dietma...@claas-solutions.de
Dietmar Kuehl wrote:
>
> Hi,
> William Roeder (bro...@titan.com) wrote:
> : 1. This is operating system dependent and not part of the standard C++
> : language.
>
> This assertion is plain false! There is a standard C++ approach to
> redirect any C++ stream. Please buy and read some reference books
> before you complain about articles! You might also want to research
> the following newsgroups before you post unpolite articles:
<snip>

I apologize if my post was unpolite, but newsgroups receive so many off
topic questions that we get tired of responding to them time after time.

Yes, I know that C++ can redirect any stream. However, stdin is not a
stream and that can not be redirected by a program. You can redirect
the stream CIN (which is normally connected to stdin) but the not the
original stdin.

I took the original question
> How do you redirect stdin <snip>
to mean redirect stdin before the program runs. That is definitely
outside C++.

> How do you redirect stdin (cin); if your program should be able to read
> input from either stdin or a file, dependent on some program argument
> (which is very common on UNIX as far as I know)

Upon rereading the question, I now see that he wants to redirect cin
depending on program arguments. This is an entirely different question
than my original understanding.
--
__ _ __ You don't have to swim faster than the
/ ) / / ' ) ) / shark, just faster than the
/--' * / / /--' _ _ /> _ / _ __ guy next to you. --anonymous

0 new messages