So I'm reading from a file into a buffer. By default, this method doesn't null terminate my string.
size_t result;
size_t total = 0;
/* Get the file size */
FILE* pfile;
pfile = fopen(filename, "rb");
fseek(pfile, 0, SEEK_END);
long lfile = ftell(pfile);
rewind(pfile);
char* file_buffer = malloc(sizeof(char) * lfile);
while ((result = fread(file_buffer, 1, lfile, pfile)) > 0)
{
total += result;
}
resp->content_length = lfile;
file_buffer[lfile] = '\0'; //so I try to null terminate it here.
But I'm getting invalid write of size 1. What am I doing wrong?
Is there another way to null terminate what's in my buffer?
lfileand you are trying to access a position outside itfile_buffer[lfile] = '\0'.