×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

defaulting ifstream variable to standard input

defaulting ifstream variable to standard input

defaulting ifstream variable to standard input

(OP)
Hi peeps

About a decade ago I wrote

CODE

    ifstream fin;

    if (NULL == inputStream)
    {
        fin.attach(0);
        inputStream = "stdin";
    }
    else
    {
        fin.open(inputStream, ios::in);
    }
inputStream was NULL unless a filename had been specified on the command line. The attach() function that was available with that compiler nearly a decade ago, when I wrote it, allowed me to use this program as part of a pipeline.

I think that's a fairly standard requirement for Unix programs so I'm hoping someone will have implemented this idea somehow with a more recent compiler. It seems the attach() function was an extension that today's standard don't have.

Cheers

==========================================
              I phoned the local ramblers club today, and this bloke just went on and on.

RE: defaulting ifstream variable to standard input

In C++ "stdin" == cin (which is already open).
If you need to read from stdin, just read from cin.

RE: defaulting ifstream variable to standard input

(OP)
I know cin is already open. That's why the attach() function was able simply to plug in the file number and the open() function was only used for a named file.

What I want is for my program to cater for a user who specifies a file on the command line and also for one who wishes to pipe the output of one command in as the input stream of this one.

sortByDate who.txt

or

who -a | sortByDate

I want to know how to map cin on to my variable fin when the program is used as a pipe.

==========================================
              I phoned the local ramblers club today, and this bloke just went on and on.

RE: defaulting ifstream variable to standard input

STL stream object does not perform real i/o, it's a job for STL streambuf class object.
Try this approach:

CODE

streambuf* pold = 0; // default cin streambuf obj ptr.
ifstream infile;     // for external text file source.
if (use_file_in_cin) // check argc or what else...
{
   infile.open(...);
   pold = cin.rdbuf(infile.rdbuf()); // attach new streambuf
}

// Now cin works with infile contents...

// Don't forget to reset initial cin streambuf object
if (pold)
   cin.rdbuf(pold);
...
So no need in old attach() function, rdbuf() can do it...

RE: defaulting ifstream variable to standard input

(OP)
I went with this

CODE

    ifstream foo(inputStream);
    istream fin(cin.rdbuf());

    if(foo)
    {
        fin.rdbuf(foo.rdbuf());
    }
as suggested by XSquared over on the CProgramming forum.

Looks like much the same suggestion as your own.

Thanks

==========================================
              I phoned the local ramblers club today, and this bloke just went on and on.

RE: defaulting ifstream variable to standard input

Without restoring cin.rdbuf you may catch an exception in cin destructor (referred to closed file rdbuf).
Yes, it's the same (standard and portable;) mechanics...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close