c++ - strlen of a char array is greater than its size. How to avoid? -


i defined array:

char arr[1480];

now, file, read 1480 chars , put arr (by doing arr[i]=c). know 1480 chars because use while loop read 1 char ar time (istream.get()) , stop when incrementing index = 1480.

however, after that, strlen(array) , returns 1512. how come? happens in occasions, not in occasions, though every time read file, read 1480 chars.

my doubt 1 char might occupy more 1 unit (units returned strlen(arr)). if so, how can fix this?

thanks

ps: asked problem earlier pointer gets garbaged, , cause when have buffer (arr) of length > 1480.

the array size must include string terminator. since read size of array can't (and don't) add string terminator. strlen function, , other string functions, uses string terminator character find end of string. if it's not there continue until finds it.

if may have no more 1480 characters in string, should have array of size 1481, last being string terminator '\0'.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -