473,406 Members | 2,387 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,406 software developers and data experts.

copy std::cin to a ifstream

Hi,

consider the following program

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]){
if (argc<2) exit(1);
ifstream infile;
if (argv[1]!="-")
infile.open(argv[1]);
else
infile=cin; //doesn't compile
// read from infile
return 0;
}

In c I can just assign stdin to a FILE*. What would be the c++ way of
doing this? Do I have to use a pointer here, too? I could use
infile.open("/dev/stdin") but that's probably not portable.

Thanks,

Ralf
Jun 23 '07 #1
3 14639
Ralf Goertz wrote:
Hi,

consider the following program

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]){
if (argc<2) exit(1);
ifstream infile;
if (argv[1]!="-")
infile.open(argv[1]);
else
infile=cin; //doesn't compile
// read from infile
return 0;
}

In c I can just assign stdin to a FILE*. What would be the c++ way of
doing this? Do I have to use a pointer here, too? I could use
infile.open("/dev/stdin") but that's probably not portable.

Thanks,

Ralf
I'd use a pointer

istream* input;
ifstream infile;
if (argv[1]!="-")
{
infile.open(argv[1]);
input = &infile;
}
else
{
input = &cin;
}
// read from *input

john
Jun 23 '07 #2
John Harrison wrote:
Ralf Goertz wrote:
>Hi,

consider the following program

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]){
if (argc<2) exit(1);
ifstream infile;
if (argv[1]!="-")
infile.open(argv[1]);
else
infile=cin; //doesn't compile
// read from infile
return 0;
}

In c I can just assign stdin to a FILE*. What would be the c++ way of
doing this? Do I have to use a pointer here, too? I could use
infile.open("/dev/stdin") but that's probably not portable.

Thanks,

Ralf

I'd use a pointer

istream* input;
ifstream infile;
if (argv[1]!="-")
{
infile.open(argv[1]);
input = &infile;
}
else
{
input = &cin;
}
// read from *input

john
If you like you could use a reference (untested code)

ifstream infile;
istream& input = pick_input(infile, argv);
istream& pick_input(char** argv, ifstream& infile)
{
if (argv[1]!="-")
{
infile.open(argv[1]);
return infile;
}
else
{
return cin;
}
}
Jun 23 '07 #3
John Harrison wrote:
John Harrison wrote:

If you like you could use a reference (untested code)

ifstream infile;
istream& input = pick_input(infile, argv);
istream& pick_input(char** argv, ifstream& infile)
{
if (argv[1]!="-")
{
infile.open(argv[1]);
return infile;
}
else
{
return cin;
}
}
That would be an idea, thanks.

I just thought of doing it the other way around, that is always using
cin for reading after reopening it with the filename in argv[1] if that
is not "-". However, std::istream doesn't have either open() or close().
FAQ 15.13 mentions that it is in principle possible to close and reopen
std::cin. But probably only in a nonstandard way. Just because I'm
curious: Is there a reason why it would be undesirable to have a program
decide on it's own what it's standard input would be?

Ralf
Jun 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: William Payne | last post by:
Hello, when I was writing a user-driven test program for a data structure I wrote, I encountered an annoying problem. The test program is laid out as a menu with several numbered options. The user...
5
by: Chris Mantoulidis | last post by:
Let's say I have this: std::string s1; std::cin >> s1; This will read s1 from cin until it finds a space (or a newline, whichever comes first). Okay this works. But when I want to continue...
3
by: yw | last post by:
Hi, When I use std::cin and std::cout to input some data, how can I ingore the cin by using the Enter key? For example: string sname; cout<<"Please input your name:"<<endl; cin>>sname; If...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
2
by: Assertor | last post by:
Hi, All. (VC++6.0) I found some strange thins when using getline() and seekg() of std::ifstream. After the file position of an open file was shift to the end of the file, seekg() did not...
8
by: junw2000 | last post by:
Below is simple code: #include <iostream> int main(){ int i; while(1){ while(!(std::cin >i)){ std::cout<<"Invalid input i: "<<i<<'\n';
5
by: Assertor | last post by:
Hi, all. Is there any way to create an instance of std::ifstream using std::string. (through std::ifstream's constructor or assignment operator or iterator, etc...) i.e. std::string str =...
12
by: pekka | last post by:
I'm trying to measure user input time with my Timer class object. It isn't as easy as I expected. When using std::cin between timer start and stop, I get zero elapsed time. For some unknown reason,...
2
by: mpalomas | last post by:
Hi C++ folks, I have trouble to open files whose path contains non-ascii characters with std::ifstream. For instance let's say i just have a file which has Japanese characters either in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.