redirecting cin

Andrew Sutton ansutton@sep.com
Tue Dec 11 09:06:00 GMT 2001


On Tuesday 11 December 2001 11:45 am, Bo Thorsen wrote:
> On Tuesday 11 December 2001 17:11, Andrew Sutton wrote:
> > MyParser parser;
> > parser.parser( some_file_stream );
>
> Hold it right there, mister. Wrong design here, the parser method should
> take a general inputstream instead of a filestream. No need for you to use
> one of the specializations when you can just work on general input.
>
> Then add an overloaded method to make an ifstream from a filename and call
> that. And you will find that if you change the argument type to istream, it
> will accept cin.

you're right. that was the right design. i changed it so i open the ifstream 
and pass it as an istream. good observation :) unfortunately, it didn't 
really solve the problem. it turns out that the redirection _was_ happening. 
i can test it by cout << cin.rdbuf() and i get the contents of the file that 
i'm parsing.

now, it seems that the issue is one of synchronization with stdin (used by 
the parser to generate and read token streams). anyway, the redirection 
happens at the streambuf layer. so, technically calls to cin >> foo will take 
data from the open ifstream.

that's all well and good - at least i figured out how to do it, but i need 
calls to fread( stdin, ... ) or read( 0, ... ) to take data from the open 
ifstream. i've tried sync()ing cin, but that didn't seem to really do 
anything. i thought it would sync the the iostream with stdio, but i guess 
that's not really what it does :)

anyother thoughts on mixing iostream redirection with stdio dups?

andrew sutton
ansutton@sep.com



More information about the Libstdc++ mailing list