Please enter at least 5 chars: cin.read() successfully read 5 characters: abcde*
Please enter at least 5 chars: cin.read() successfully read 5 characters:
abcd*
Please enter at least 5 chars: cin.read() successfully read 5 characters:
abcd*
Please enter at least 5 chars: cin.read() was unsuccessful
I have the following queries:
1) For the 3rd read() function execution, the I/P buffer was abcdef. Yet the O/P shows abcd*. It should have been abcde*, since 5 characters are being read. Why is this happening?
2) We are told that read() doesn't append a terminator. Why is the * displayed then? Is that because of the cout << statement?
> We are told that read() doesn't append a terminator. Why is the * displayed then?
There is undefined behaviour; the array is uninitialised and need not hold a null terminated byte string.
Initialise the array on line 8: char buf[80] {} ; // initialise to all zeroes
Are you leaving '\n' in the input buffer each time? That would upset your calculations.
Also, when you cout a char* , you get everything until the first zero is hit. So you could get 5kb of data dumped out, and then a crash. You could get 10 characters. You don't know what you're going to get, so if you want to know for sure, you should do something about that so that you only see the characters written into it, rather than whatever random junk happens to be in the memory.
Also, I expect that the memory used for the char buf[80]; is the exact same memory each time, so you'll see anything you wrote in there previously there again, unless you overwrite it. This is another way to make it difficult to see what's going on.
I suggest that you fill the buffer with zeros at the start of your read function.
I've put in a diagnostic message that uses cin's peek() function. This function returns an ASCII value which is cast to a char. The diagnostic shows what the next character in the buffer is.
I had also tried to discard the '\n' using:
cin.ignore(1, '\n');
However, the results weren't good. Hence, I used the peek(), which shows what's happening behind the scenes.