After reading in s0 stream is in the EOF state. Therefore, the next read is not performed if the EOF state is not cleared. Writing to a stream does not clear the read state for you.
Edit only to complete the answer. The behavior comes from the definition of eofbit of ios_base::iostate , which states that the state of the stream will have this bit if the stream is at the end of the input sequence.
In the first version of your program, since the EOF state is not cleared after the first read in s0 , neither the second read nor the third read will be executed. So, the failure of the first read leaves s1 empty, and the failed second read leaves s0 unchahnged.
In the second version of your program, you clear ss after the first read in s0 , which allows you to perform a second read in s1 . However, after the second read, the thread is again in the EOF state, so the third read is not performed. This leaves s0 unchanged.
jxh
source share