I love handling null terminated strings :))))

I have no idea how this didn't come up earlier
This commit is contained in:
Nat 2023-01-20 16:45:08 -08:00
parent 02c7492f74
commit 6c402c771f
Signed by: nat
GPG Key ID: B53AB05285D710D6
1 changed files with 3 additions and 4 deletions

View File

@ -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