C++ input stream - but how does it know? - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: December 30, 2024, 11:37:22 am
content: I've started with C++ and my first look at files and file/IO streams has me reading a file that represents hours and temperature:0 60.71 60.62 60.33 59.22A vector is created:Code:struct Reading { // a temperature readingint hour; // hour after midnight [0:23]double temperature; // in Fahrenheit};and readings are read in after the file stream is created:Code:while (my_input_stream >> hour >> temperature) {if (hour < 0 || 23 <hour)perror("hour out of range");temps.push_back(Reading{hour,temperature});}Now what is bugging me is the "my_input_stream >> hour >> temperature" above. If I understand correctly the way this has to work is that given the int and double types of the structure elements the resulting code will take the space to mean the end of a number and is of course implicitly converting the characters of the text into numbers also given the types.This is I take it as normal as it is unspoken in the code and text of Bjarne Stroustrup's book?I never quite understood why he chose in the book to not use perror() and instead created an error() function that is referenced much earlier in the book but works either way... when you google it and find out what is going on from previous people who have tried.

author: golden_labels
author_rank: Super Contributor
timestamp: December 30, 2024, 01:16:57 pm
content: Operator overloading and, in particular, looking at argument types inoverloads for << and >>may help you understand, what’s going under the hood.In short: those are just functions, which accept a reference to the stream and a reference to the variable. There is a separate overload for each type:int,double, and so on. Each is a function, that has its own internal logic of parsing data and puts the result into the variable it received as the argument.The entirestream >> a >> bexpression may be seen as equivalent to:read(stream, a); read(stream, b).(1)All variables passed as references, soreadmay put values into them.As for the Stroustrup’s book: I can’t comment, as I don’t remember that anymore. But note that your use ofperrorfunction is invalid there.perror, aside from being a C relic, is meant to reporterrnocodes. You can’t use it arbitrarily to just write any text.(1)More accurately this should be:read(read(stream, a), b). That’s because the result of firststream >> ais a stream reference itself, which is passed directly to the next>>invocation.

author: Simon
author_rank: Unknown
timestamp: December 30, 2024, 02:50:29 pm
content: Quote from: golden_labels on December 30, 2024, 01:16:57 pmI see, the function defined in the book is:Code:void error(string s){throw runtime_error(s) ;}so essentially I expect it to be the same as just using the one line of that functions code, just quicker to type. There is some header file he wrote that is supposed to simplify some things but prompts many questions online.

author: golden_labels
author_rank: Super Contributor
timestamp: December 30, 2024, 03:04:07 pm
content: Two explanations come into mind. It’s an example he uses or modifies later, which is being introduced in a simplified (and useless) form earlier. Or because he sees that as a good practice, that may aid later debugging or code re-use.

author: TheCalligrapher
author_rank: Regular Contributor
timestamp: December 30, 2024, 03:53:21 pm
content: Quote from: Simon on December 30, 2024, 11:37:22 amIt is not clear what specific behavior your question is about."Take the space to mean the end of a number"? But that's how formatted input worked since the beginning of times. That's how it works in C and virtually everywhere else. It has nothing to do with C++ specifically."Implicitly converting the characters of the text into numbers also given the types"? But that's once again, how formatted input worked since the beginning of times. That's the whole idea offormatted input. The only difference is that in C we had to specify the target type explicitly (say, through the format string in `scanf`), while in C++ function overloading produces the appearance of automatic type recognition.Quote from: Simon on December 30, 2024, 11:37:22 amAgain, what specifically is your question about? What is the focal point here? What is "unspoken"?Quote from: Simon on December 30, 2024, 11:37:22 amHow does it knowwhatspecifically?

author: xvr
author_rank: Frequent Contributor
timestamp: December 30, 2024, 05:09:37 pm
content: Small overview of what happened inside 'while' (my_input_stream >> hour >> temperature):Code:std::istream& stream1 = operator >> (my_input_stream, hour); // 'hour' here taken by-reference and filled by 'operator >>'. Returned value from 'operator >>' is reference to first parameterstd::istream& stream2 = operator >>(stream1, temperature);bool condition_for_while = stream2.operator bool(); // 'operator bool' return 'good' status of stream. Reading past EOF considered not good :)

author: Simon
author_rank: Unknown
timestamp: December 30, 2024, 06:30:23 pm
content: Quote from: TheCalligrapher on December 30, 2024, 03:53:21 pmThis standard library stuff is all new to me. I have spent 3 years learning to program micro-controllers in C and not really using libraries very much. With the intention of learning more about the wider world of programming and from a PC/OS environment point of view I am 'also learning C++. "Programming principles and practice using C++" seems to be a good balance of both the language and how to operate in the environment and deal with common situations beyond just the language syntax.

author: IanB
author_rank: Super Contributor
timestamp: December 30, 2024, 06:54:56 pm
content: Quote from: Simon on December 30, 2024, 06:30:23 pmYes, that is the difference between programming for (small) microcontrollers, and programming for desktop environments.On the desktop, standard libraries are everything. They encapsulate commonly re-used code into highly reliable, optimized, and fully debugged implementations, so you don't have to hand-roll code for things that are done frequently by all programmers.One thing that C++ tries to do is to make the code look simpler and less cluttered to read. It succeeds in this, but it also ends up hiding a lot of the machinery that makes everything work.In languages like Python, some of the things you will find in a C++ standard library are core parts of the language itself and you don't even need a library for them.