Author Topic: C++ input stream - but how does it know?  (Read 934 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18210
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
C++ input stream - but how does it know?
« on: December 30, 2024, 11:37:22 am »
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: [Select]
struct Reading { // a temperature reading
        int hour; // hour after midnight [0:23]
        double temperature; // in Fahrenheit
    };

and readings are read in after the file stream is created:

Code: [Select]
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.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1628
  • Country: pl
Re: C++ input stream - but how does it know?
« Reply #1 on: December 30, 2024, 01:16:57 pm »
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.
People imagine AI as T1000. What we got so far is glorified T9.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18210
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: C++ input stream - but how does it know?
« Reply #2 on: December 30, 2024, 02:50:29 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.


I see, the function defined in the book is:

Code: [Select]
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.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1628
  • Country: pl
Re: C++ input stream - but how does it know?
« Reply #3 on: December 30, 2024, 03:04:07 pm »
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.
People imagine AI as T1000. What we got so far is glorified T9.
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 161
  • Country: us
Re: C++ input stream - but how does it know?
« Reply #4 on: December 30, 2024, 03:53:21 pm »
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.

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.

This is I take it as normal as it is unspoken in the code and text of Bjarne Stroustrup's book?

Again, what specifically is your question about? What is the focal point here? What is "unspoken"?

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

How does it know what specifically?
« Last Edit: December 30, 2024, 04:07:08 pm by TheCalligrapher »
 
The following users thanked this post: Siwastaja, eutectique

Offline xvr

  • Frequent Contributor
  • **
  • Posts: 640
  • Country: ie
    • LinkedIn
Re: C++ input stream - but how does it know?
« Reply #5 on: December 30, 2024, 05:09:37 pm »
Small overview of what happened inside 'while' (my_input_stream >> hour >> temperature):
Code: [Select]

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 parameter
std::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 :)
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18210
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: C++ input stream - but how does it know?
« Reply #6 on: December 30, 2024, 06:30:23 pm »
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.

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.

This is I take it as normal as it is unspoken in the code and text of Bjarne Stroustrup's book?

Again, what specifically is your question about? What is the focal point here? What is "unspoken"?

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

How does it know what specifically?


This 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.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12616
  • Country: us
Re: C++ input stream - but how does it know?
« Reply #7 on: December 30, 2024, 06:54:56 pm »
This 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.

Yes, 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf