By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,433 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,433 IT Pros & Developers. It's quick & easy.

Selecting between file and cout depending on parameters

P: n/a
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.
Oct 27 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tayfun Ozdemir wrote:
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

--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Oct 27 '05 #2

P: n/a
Stefan Nšwe wrote:
Tayfun Ozdemir wrote:
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.
Oct 27 '05 #3

P: n/a
Tayfun Ozdemir wrote:
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
--
Salu2
Oct 27 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.