[comp.lang.c++] Help: input/output thru' command line parameters

raghu@maxwell.Concordia.CA ( RAGHU PRASAD CHALASANI ) (12/01/90)

	I am new to c++, though I've been using c for a long time.
In C, one can use command line parameters to specify whether input
is taken from a input file or from the std. terminal. one would
typically do it as:
main ( int argc, char *argv [])
{
	FILE *fin, *fout, *fopen ();

	if (argc == 1)
	{
		fin = stdin;
		fout = stdout;
	}
	else
	{
		fin = fopen (*++argv, "r");
		fout = fopen (*++argv, "w");
	}
	.
	.
	then to read input
	fscanf (fin, "...", ..);
}

I donot want to use fscanf or fprintf in c++. So is there a
similar way (or better way) of doing it using
'cout' and 'cin' in c++.
please send me email as i don't read this very often.

thanks
raghu

email: raghu@davinci.concordia.ca

jfischer@sco.COM (Jonathan A. Fischer) (12/03/90)

	Many people might be interested in the answer, so I'm posting
as well as responding (not to mention that it's rather poor net
etiquette to say "just respond to me, I can't be bothered reading this
newsgroup for the next couple of days").

	Redirecting cin/cout is simple:

        ifstream        infile( "infile" );
        ofstream        outfile( "outfile" );

        cin = ifstream;
        cout = ofstream;

        cin >> something;	// Comes from infile
        cout << something;	// Goes to outfile

        If you want to restore cin and cout after doing the redirection,
you can do the following:

        istream_withassign      save_cin;
        ostream_withassign      save_cout;

        save_cin = cin;
        save_cout = cout;

        // Redirect cin and cout
        // ...

        // Restore cin and cout
        cin = save_cin;
        cout = save_cout;