Selecting between file and cout depending on parameters

T

Tayfun Ozdemir

Hello there,

In my program If user enters console parameters I will use first
parameter as input file. If user does not enter anything I will use
default (cin).

If I were programming in C I would do that by using global FILE
pointers. Is there any better C++ looking way to that?

Example:
..
..
..
if (argc > 1) {
// use argv[1] file in all modules(cpp files)
}
else {
// continue using cin
}

Thank you so much for your time.
Tayfun Ozdemir.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Tayfun said:
Hello there,

In my program If user enters console parameters I will use first
parameter as input file. If user does not enter anything I will use
default (cin).

If I were programming in C I would do that by using global FILE
pointers. Is there any better C++ looking way to that?

Example:
.
.
.
if (argc > 1) {
// use argv[1] file in all modules(cpp files)
}
else {
// continue using cin
}

Thank you so much for your time.
Tayfun Ozdemir.

//------------------------------------------
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
ofstream of;
streambuf* cout_sb = cout.rdbuf();

if(argc > 1)
{
cout.rdbuf(of.rdbuf());
of.open(argv[1]);
}


cout << "This goes to std::cout" << endl;

cout.rdbuf(cout_sb);
return 0;
}
//------------------------------------------


HTH


Stefan
 
T

Tayfun Ozdemir

Stefan said:
Tayfun said:
Hello there,

In my program If user enters console parameters I will use first
parameter as input file. If user does not enter anything I will use
default (cin).

If I were programming in C I would do that by using global FILE
pointers. Is there any better C++ looking way to that?

Example:
.
.
.
if (argc > 1) {
// use argv[1] file in all modules(cpp files)
}
else {
// continue using cin
}

Thank you so much for your time.
Tayfun Ozdemir.


//------------------------------------------
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
ofstream of;
streambuf* cout_sb = cout.rdbuf();

if(argc > 1)
{
cout.rdbuf(of.rdbuf());
of.open(argv[1]);
}


cout << "This goes to std::cout" << endl;

cout.rdbuf(cout_sb);
return 0;
}
//------------------------------------------


HTH


Stefan

Thanx for your reply.
I am really greatful.
Tayfun Ozdemir.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Tayfun said:
In my program If user enters console parameters I will use first
parameter as input file. If user does not enter anything I will use
default (cin).

If I were programming in C I would do that by using global FILE
pointers. Is there any better C++ looking way to that?

I use something like that:

istream input (cin.rdbuf () );
ifstream ifile;
if (! filename.empty () )
{
if (! ifile.open (filename.c_str (), ios::in) )
throw some_error;
input.rdbuf (ifile.rdbuf () );
}
// Use input
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top