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 ...

+ Reply to Thread
Results 1 to 4 of 4

copy std::cin to a ifstream

  1. Default 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

  2. Default 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

  3. Default 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;
    }
    }

  4. Default 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

+ Reply to Thread

Similar Threads

  1. ifstream/string ctor
    By Application Development in forum c++
    Replies: 4
    Last Post: 12-18-2007, 04:07 AM
  2. Getting Error Text from ifstream
    By Application Development in forum c++
    Replies: 6
    Last Post: 11-21-2007, 05:03 AM
  3. Help needed for STL ifstream class
    By Application Development in forum c++
    Replies: 12
    Last Post: 10-06-2007, 06:35 AM
  4. ifstream from stdin
    By Application Development in forum c++
    Replies: 4
    Last Post: 07-12-2007, 02:02 PM
  5. ifstream eof not reporting eof?
    By Application Development in forum c++
    Replies: 10
    Last Post: 06-15-2007, 03:50 AM