I have a file that i want to read from and store the data to a char array that i dynamically allocate memory to after each character read. I want to dynamically allocate memory exactly to that of the size needed. Here is what i have:
FILE *fp;
char *data;
int c=0;
fp=fopen("home/bob/Downloads/filename", "r");
data=malloc(sizeof(char));
do{
data[c]=fgetc(fp);
printf("data : %c\n", data[c]);
c++;
data=realloc(data, sizeof(char)+c);
} while(data[c]!=EOF);
I get a segmentation fault.