Fix read from file

This commit is contained in:
Raffaele Mignone 2021-03-25 19:00:59 +01:00
parent c25a0a5045
commit f312ca2205
Signed by: norangebit
GPG Key ID: F5255658CB220573
4 changed files with 6 additions and 6 deletions

1
data/pattern.txt Normal file
View File

@ -0,0 +1 @@
mamma

1
data/text.txt Normal file
View File

@ -0,0 +1 @@
testo di esempio mamma altra mamma esempio prova mammamma esempio mamma

7
main.c
View File

@ -100,11 +100,8 @@ void find_end(int residue, char *pattern, char *text, int **matches, int *match_
}
void initialize() {
text = "testo di esempio mamma altra mamma esempio prova mammamma esempio mamma";//read_file("data/text.txt", &text_len);
pattern = "mamma";//read_file("data/pattern.txt", &pattern_len);
text_len = strlen(text);
pattern_len = strlen(pattern);
text = read_file("data/text.txt", &text_len);
pattern = read_file("data/pattern.txt", &pattern_len);
printf("text: %s\n", text);
printf("pattern: %s\n", pattern);

3
util.c
View File

@ -37,8 +37,9 @@ char *read_file(char *filepath, int *len) {
*len = ftell(f);
fseek(f, 0, SEEK_SET);
char *content = (char *) malloc(sizeof(char) * (*len + 1));
char *content = (char *) malloc(sizeof(char) * (*len));
fread(content, 1, *len, f);
*len = *len - 1;
content[*len] = '\0';
fclose(f);