473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Selecting between file and cout depending on parameters

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
3 1317
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Simon Leonard | last post by:
I'm developing an API that I want be as user friendly as possible. My problem is that there are functions that can take many optional parameters (of various type) and I want to be able to specify...
2
by: Alexander Schmidt | last post by:
Hi, I am not very familiar with C++ programming, so before I do a dirty hack I ask for a more elegant solution (but only the usage of STL is allowed, no special libs). So I need to read a file...
6
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use....
7
by: B. Williams | last post by:
I wrote a program that would simply output data to the screen, but now I am trying to have the data saved to a file. I don't have a problem creating the file or even outputting data to it when there...
10
by: ConfusedAlot | last post by:
I am having difficulty defining the function prototypes and corresponding parameters when the function is called from main. Any help is greatly appreciated. //The database struct OpAmps { ...
9
by: Generic Usenet Account | last post by:
I had a need to randomly select an element from an STL collection. It does not appear that this functionality is provided out-of-the-box with STL. Here is my crude implementation. I am using...
4
by: Sanchit | last post by:
I want to know thta how can i edit a file in C++ For Example my file is Mr XyZ FFFFFF 65 And now i want go change this number 65 to 87.... how can i Do this..... I...
1
by: r3d dra6on | last post by:
I am trying to get the file size of a memory mapped I/O from the input file, and set that size to the output file. Right now the code below is stuck on the kernel mapping for the output. The...
0
by: grejon04 | last post by:
I'm trying to write a script that will look into a directory, and check each entity to see if it is a directory or a file. (Borland CodeGear RAD Studio) Here's what I have so far. #include...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.