I am trying to read a file into a dynamically allocated 2d array but I keep getting the same line printed out. The file I am putting into this code is a .txt with 1000 lines. The print at the end is to check if the array is getting the right data. And lastly I am trying to realloc as the code goes to fit the number of lines to put into arrays. THANKSS :)
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main(void)
{
char buffer[349];
char *del = "";
int i = 0;
int count = 0;
int len = 100;
int row = 10;
int max = 10;
char** d;
d = malloc(row * sizeof(char*));
for(i = 0; i < row; i++)
{
d[i] = malloc(349);
}
i = 0;
while (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
d[i] = buffer;
i++;
if(i == max)
{
d = realloc(d, sizeof(d)*2);
for(i = max; i <= max*2; i++)
{
d[i] = malloc(349);
}
max = max * 2;
printf("reallocating to %d\n", max);
}
}
for(i = 0; i < 20; i++)
{
printf("%s\n", d[i]);
printf("%lu\n", sizeof(d[i]));
}
}
Sample line on txt file:
2152,1,MAIN,SOCW,6390,006,22913,IND - Independent Study,0,1,1,0,0,12:00 AM,12:00 AM,N,N,N,N,N,N,N,01-20-2015,05-08-2015
And there are about 1000 lines like these in the file.