Hello everybody!
I have a query regarding the code below. Here within while loop, there is a line buffer[chars_read-1] = '\0'. Now I understand that this is being done in order to make char array buffer a string. My question is that why do we use chars_read - 1 ? won't this overwrite the last character in the array?
Code:
if(fp_read != NULL) {
chars_read = fread(buffer, sizeof(char), sizeof(buffer), fp_read);
while(chars_read > 0) {
buffer[chars_read - 1] = '\0';
printf("Output:-\n%s\n", buffer);
chars_read = fread(buffer, sizeof(char), sizeof(buffer), fp_read);
}
}