Enter some text and hit <CTL-C>.
Stepping through the code shows the vector is empty and gcount() has returned a value of 0. |
that's the expected result of hitting <Ctrl-C>: canceling any input and returning zero bytes.
If you wanted std::cin.read to write something to that vector, hit <Enter>, and then <Ctrl-Z>, and then <Enter> again. That's how Windows console signals end of input.
Alternatively, redirect the input from a file, the end of file will then signal the end of input
Alternatively, type more characters than the size submitted to cin.read() and press Enter
std::cin.read(buf.data(), sizeof(buf)); |
That uses the wrong size: use buf.size() instead of sizeof(buf)
this is also wrong: this checks the success of a read before attempting it instead of after, but it's not a big deal in your case (you have to be somewhat unlucky to hit the condition where it loops one more time due to this mistake, and even then you're recording and presumably checking gcount). Still, use proper C++:
1 2
|
while(std::cin.read(buf.data(), buf.size())) { readcount =
...
|
- all iostream read operations can be used as boolean conditions to test their success *after* the attempted read.