I am getting a segmentation fault while trying to read from a file into a 2D dynamic array. I know this is a little vague of a question, but this being my first C code, I haven't been able to trace out what the problem is. Any direction will be appreciated. These are my variables:
int i;
int rowsize=0;
int colsize=0;
int colenter=0;
int rowenter=0;
int colexit=0;
int rowexit=0;
char sizestring[20];
char enterstring[20];
char exitstring[20];
int line=0;
int j;
int k;
char**mazearray;
FILE*file;
And this is my allocation.
mazearray = (char**) malloc(sizeof(char*)*rowsize);
for(i =0; i<rowsize; ++i)
{
mazearray[i]= (char*)malloc(sizeof(char)*colsize);
}
And this is me trying to read from a file and parse the information. I first try to read in 3 lines of strings which contain the information about a maze (Array size, enterance, exit etc.) and then try to fill out the 2d array with the for loop at the bottom.
file = fopen(argv[1],"r");
if (!file)
{
printf("The file is not connected/n");
}
while (!feof(file))
{
while(line<3)
{
if(line==0)
{
fgets(sizestring,20,file);
sscanf(sizestring,"%d,%d", &colsize, &rowsize);
line++;
}
else if(line ==1)
{
fgets(enterstring,20,file);
sscanf(enterstring,"%d,%d",&colenter, &rowenter);
line++;
}
else if(line ==2)
{
fgets(exitstring,20,file);
sscanf(exitstring, "%d,%d", &colexit, &rowexit);
line++;
}
}
for(j=0; j<rowsize; j++)
{
for(k=0; k<colsize; k++)
{
fscanf(file,"%c",&mazearray[j][k]);
}
}
}
fclose(file);
Again any direction will help. Thanks