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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -