Elysia, master5001, thank you for your answers.
master5001, I tried your function but couldn't get it to work properly. Here's what I did first:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ftoarray.h"
main () {
FILE *file;
if (!(file = fopen("testi.apps","r"))) {
puts("\nerror opening file\n");
exit(1);
}
if ((ftoarray(file))==NULL) {
puts("\nerror reading file to an array\n");
exit(1);
}
fclose(file);
...and got a segfault. Inspecting the function further I noticed that the size of size_t size was not assigned correctly. From
Code:
fseek(file, 0, SEEK_SET);
size = fseek(file, 0, SEEK_END);
fseek(file, 0, SEEK_SET);
I changed the code to
Code:
fseek(file, 0, SEEK_SET);
fseek(file,0,SEEK_END);
size = ftell(file);
fseek(file,0,SEEK_SET);
After I fixed this another problem arised - the fread() fails:
Code:
if(fread(whole, size, 1, file) != size)
{
free(whole);
return NULL;
}
However, I don't really understand why the return value of fread is being compared to size since as far as I understand it fread is supposed to return the nitems read not characters read(where the number of items read would be the third argument of fread). Checking that fread returns nitems instead of size to make it pass, the function still fails to a segmentation fault.
Please advise. Thanks.