summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xfile_reader.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/file_reader.c b/file_reader.c
index eea7761..c262091 100755
--- a/file_reader.c
+++ b/file_reader.c
@@ -6,19 +6,17 @@ File* file_open(const char* filename)
6{ 6{
7 File* retval = malloc(sizeof(File)); 7 File* retval = malloc(sizeof(File));
8 retval->name = filename; 8 retval->name = filename;
9 retval->data = malloc(0); 9
10 retval->size = 0; 10 FILE* file = fopen(retval->name, "rb");
11 FILE* file = fopen(filename, "rb"); 11 fseek(file, 0, SEEK_END); // go to the end of the file
12 if(file) 12 if(file)
13 { 13 {
14 do 14 retval->size = ftell(0); // save the position (end of the file)
15 { 15 retval->data = malloc(retval->size + 1);
16 retval->data = realloc(retval->data, retval->size + BUFFER_SIZE); 16 fseek(file, 0, SEEK_SET); // return to the beginning of the file
17 retval->size += fread(&retval->data[retval->size], 1, BUFFER_SIZE, file); 17 fread(retval->data, retval->size, 1, file);
18 } while(!feof(file));
19 retval->data = realloc(retval->data, retval->size);
20 fclose(file); 18 fclose(file);
21 return retval; 19 return retval; // file is completely stored in the buffer
22 } 20 }
23 else 21 else
24 { 22 {
@@ -29,8 +27,11 @@ File* file_open(const char* filename)
29 27
30void file_close(File* file) 28void file_close(File* file)
31{ 29{
32 free(file->data); 30 if(file)
33 free(file); 31 {
32 free(file->data);
33 free(file);
34 }
34} 35}
35 36
36int main() 37int main()
..