Products > Programming

C++ input stream - but how does it know?

(1/2) > >>

Simon:
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.7
1 60.6
2 60.3
3 59.22

A vector is created:


--- Code: ---struct Reading { // a temperature reading
        int hour; // hour after midnight [0:23]
        double temperature; // in Fahrenheit
    };

--- End code ---

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});
    }

--- End code ---

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.

golden_labels:
Operator overloading and, in particular, looking at argument types in overloads 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 entire stream >> a >> b expression may be seen as equivalent to: read(stream, a); read(stream, b).(1) All variables passed as references, so read may 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 of perror function is invalid there. perror, aside from being a C relic, is meant to report errno codes. 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 first stream >> a is a stream reference itself, which is passed directly to the next >> invocation.

Simon:

--- Quote from: golden_labels on December 30, 2024, 01:16:57 pm ---

As for the Stroustrup’s book: I can’t comment, as I don’t remember that anymore. But note that your use of perror function is invalid there. perror, aside from being a C relic, is meant to report errno codes. You can’t use it arbitrarily to just write any text.


--- End quote ---

I see, the function defined in the book is:


--- Code: ---void error(string s)
{
    throw runtime_error(s) ;
}

--- End code ---

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.

golden_labels:
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.

TheCalligrapher:

--- Quote from: Simon on December 30, 2024, 11:37:22 am ---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.
--- End quote ---

It 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 of formatted 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 am ---This is I take it as normal as it is unspoken in the code and text of Bjarne Stroustrup's book?
--- End quote ---

Again, 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 am ---C++ input stream - but how does it know?
--- End quote ---

How does it know what specifically?

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod