Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

defaulting ifstream variable to standard input

Status
Not open for further replies.

risby

Programmer
Feb 27, 2002
84
0
0
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

==========================================
toff.jpg
I phoned the local ramblers club today, and this bloke just went on and on.
 
In C++ "stdin" == cin (which is already open).
If you need to read from stdin, just read from cin.
 
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.

==========================================
toff.jpg
I phoned the local ramblers club today, and this bloke just went on and on.
 
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...
 
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

==========================================
toff.jpg
I phoned the local ramblers club today, and this bloke just went on and on.
 
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...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top