Istream Tv

0 views
Skip to first unread message

Leontina Heidgerken

unread,
Aug 4, 2024, 4:47:06 PM8/4/24
to tiodisfiper
Theclass template basic_istream provides support for high level input operations on character streams. The supported operations include formatted input (e.g. integer values or whitespace-separated characters and characters strings) and unformatted input (e.g. raw characters and character arrays). This functionality is implemented in terms of the interface provided by the underlying basic_streambuf class, accessed through the basic_ios base class. The only non-inherited data member of basic_istream, in most implementations, is the value returned by basic_istream::gcount().

I am following a tutorial from =3144 for the assigned project. Problem is that the project spec asks that the spellchecker will be called through main by this function: void spellCh(istream& inf, istream& wordlistfile, ostream& outf)whereas the tutorial uses iftream and ofstream. So when I try this


std::istream and std::ostream are direct base classes of std::ifstream and std::ofstream respectively. Due to something called polymorphism, a derived class is also considered its base class and can be implicitly covered to a pointer or reference thereto (upwards conversion). Because of this, one can simply pass these instances to the parameters of the functions and it will successfully be converted by the compiler to their base class' type.


The file contains [1 2 3 4 a 5 6 7], and the program naturally goes into infinite loop.Okay, easy guess, peek() doesn't consume the char 'a', and maybe in >> inNum also failed to consume it somehow. No biggie, I'll just try something that does.


Weirdly enough, the approach above worked for a previous program where I had to read a triplet of data entries on a line of the format: string int int

The only difference is I used an ifstream object for that one, and an istream object for this one.


but this has a series of problems. Imagine there are many, possibly virtual, methods/functions which expect std::istream& passed as an argument.This would mean I have to do the "housework" of checking for EOF in each of them, possibly with different type of something variable, or create some weird wrapper which would handle the scenario of calling the input methods.


There are good reasons for which there is no isEof function: it is hard to specify in an usable way. For instance, operator>> usually begin by skipping white spaces (depending on a flag) while some other input functions are able to read space. How would you isEof() handle the situation? Begin by skipping spaces or not? Would it depend on the flag used by operator>> or not? Would it restore the white spaces in the stream or not?


Try with an istream* instead. Note, however, that you have to change your code slightly. Using pointers you have to preserve the memory area of the object that you're pointing. In other words, the "inFile" variable cannot be declared there, as it won't exist out of the else. The code could be, then:


An alternative design is to write your code using cin, and then use input redirection when running the program if you want to accept input from a file. This doesn't answer your exact question, but it is a simpler design for the case you presented.


Can you humor me for a second and try calling 'view.begin()' directly before using std::ranges::begin? (I'm suspicious that we're somehow not triggering instantiation of istream_view until too late; calling 'view.begin()' earlier would resolve the problem if that's the case.)


My needs includes reading from child stdout and stderr. I want to implement this as streams. If I would use simple std::streambuf descendant that reads directly from pipe on underflow - then my stdout and stderr streams would block on read when there is no data available. I want to evade this, and so far I have two ideas


Provide a timeouted method for my streams, that checks if there any data. I need only binary interface of streams i.e. read and write methods, so method which which returns available data amount is fine enough.


But wait - there is already method, with behavior which almost match my desire. std::streambuf::in_avail. What if I'll start background thread which will feed data from pipes to my streambuf? Then I could use standard stream method std::istream::readsome.

3a8082e126
Reply all
Reply to author
Forward
0 new messages