copy std::cin to a ifstream - c++
This is a discussion on copy std::cin to a ifstream - c++ ; 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 ...
-
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
-
Re: copy std::cin to a ifstream
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
-
Re: copy std::cin to a ifstream
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;
}
}
-
Re: copy std::cin to a ifstream
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
Similar Threads
-
By Application Development in forum c++
Replies: 4
Last Post: 12-18-2007, 04:07 AM
-
By Application Development in forum c++
Replies: 6
Last Post: 11-21-2007, 05:03 AM
-
By Application Development in forum c++
Replies: 12
Last Post: 10-06-2007, 06:35 AM
-
By Application Development in forum c++
Replies: 4
Last Post: 07-12-2007, 02:02 PM
-
By Application Development in forum c++
Replies: 10
Last Post: 06-15-2007, 03:50 AM