copy std::cin to a ifstream

R

Ralf Goertz

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
 
J

John Harrison

Ralf said:
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
 
J

John Harrison

John said:
Ralf said:
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;
}
}
 
R

Ralf Goertz

John said:
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
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top