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)
| 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
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.
fstream includes iostream. (Not that this is of any great
consequence... :))
I haven't tried it myself, but I believe this will work:
filebuf fb;
cin.rdbuf( fb.open( filename, ios_base::in ) );
Seth Jones
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
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
: 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
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++
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