Istream get() null terminated wording

Hello. I learning istream get() but do but understand wording of explanation. Could someone please help.

It say

Istream &get(char *buf, size n)

"The array pointed to by buf will be null terminated by get()"

But what does this mean. I know that a char array is null terminated but what does it mean it is null terminated by get().
Extracts characters from the stream and stores them in buf as a c-string, until either (n-1) characters have been extracted or the delimiting character is encountered: the delimiting character being either the newline character ('\n') or delim (if this argument is specified).
The delimiting character is not extracted from the input sequence if found, and remains there as the next character to be extracted from the stream (see getline for an alternative that does discard the delimiting character).
A null character ('\0') is automatically appended to the written sequence if n is greater than zero, even if an empty string is extracted.
I read and understood that all, but it is just the one part I say that I do not understand. Or is that part explained in what you show and I do not understand or see it?
If you understand c null-terminated strings, then what are you not understanding? When .get() is executed, the memory pointed to by buf will be a null-terminated c-style string. .get() will add a null to the read chars.
Sorry I struggling with wording sometimes. Will the array not be null terminated already. Why does get() make it null terminated. Examples

1
2
3
4
5
6
 
char str[256];
cin.get(str, 255)

court << str;


Is str not null terminated when made. Why get() make it null terminated?
char str[256] defines str as a char array of 256 chars. As it is not initialised it's contents are whatever happens to be in memory where str points to. No assumptions can be made about this.

If you do:

 
char str[256] {}


then str is default initialised to the default value for char which is 0.

If the file contains 123456 and say str already contains abcdefgh/0 and .get() didn't null-terminate the string, then after .get(str, 3) str would be 123defgh - which isn't what is wanted. As .get() null terminates you get 123/0efgh which is as wanted.
Topic archived. No new replies allowed.