Fix #2
I love handling null terminated strings :)))) I have no idea how this didn't come up earlier
This commit is contained in:
parent
02c7492f74
commit
6c402c771f
|
@ -35,7 +35,7 @@ char * read_file(char *filename) {
|
||||||
rewind(file); // Go back to the beginning of the file
|
rewind(file); // Go back to the beginning of the file
|
||||||
|
|
||||||
// Allocate enough space in our buffer to hold the entire file.
|
// Allocate enough space in our buffer to hold the entire file.
|
||||||
char *buffer = malloc(fileLength);
|
char *buffer = malloc(fileLength + 1);
|
||||||
|
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
printf("Warning: Failed to allocate enough memory for %s\n", filename);
|
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);
|
fread(buffer, 1, fileLength, file);
|
||||||
|
buffer[fileLength] = 0;
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
|
@ -335,9 +336,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
// Save the content string for later by mallocing it
|
// Save the content string for later by mallocing it
|
||||||
char *contentBuffer = endOfFirstLine;
|
char *contentBuffer = endOfFirstLine;
|
||||||
currentPage->content = malloc(
|
currentPage->content = calloc(strlen(buffer), sizeof(char));
|
||||||
sizeof(char) * (strlen(buffer) - titleLength + 1)
|
|
||||||
);
|
|
||||||
strcpy(currentPage->content, contentBuffer);
|
strcpy(currentPage->content, contentBuffer);
|
||||||
|
|
||||||
// Copy the first line (title) into its respective field
|
// Copy the first line (title) into its respective field
|
||||||
|
|
Loading…
Reference in New Issue