diff --git a/calathea.c b/calathea.c index ccd1a7d..324a1f7 100644 --- a/calathea.c +++ b/calathea.c @@ -35,7 +35,7 @@ char * read_file(char *filename) { rewind(file); // Go back to the beginning of the file // Allocate enough space in our buffer to hold the entire file. - char *buffer = malloc(fileLength); + char *buffer = malloc(fileLength + 1); if (buffer == NULL) { printf("Warning: Failed to allocate enough memory for %s\n", filename); @@ -44,6 +44,7 @@ char * read_file(char *filename) { } fread(buffer, 1, fileLength, file); + buffer[fileLength] = 0; fclose(file); return buffer; @@ -335,9 +336,7 @@ int main(int argc, char *argv[]) { // Save the content string for later by mallocing it char *contentBuffer = endOfFirstLine; - currentPage->content = malloc( - sizeof(char) * (strlen(buffer) - titleLength + 1) - ); + currentPage->content = calloc(strlen(buffer), sizeof(char)); strcpy(currentPage->content, contentBuffer); // Copy the first line (title) into its respective field