while(output_read == false)
{
...
fscanf(input_filep, "%s", &output_file);
output_filep = fopen(output_file, "w");
if(output_filep != NULL)
{
output_read = true;
}
else
{
fprintf(stderr, "ERROR: output file name not in file");
input_filep = NULL;
free(input_filep);
free(output_filep);
return 1;
}
}
fprintf(output_filep, "testing opening output, success\n");
int i;
int **boardpp;
boardpp = (int**)malloc(sizeof(int) * num_rows);
if(boardpp == NULL)
{
fprintf(stderr, "ERROR: Array of pointers for 2-D array could not be allocated\n");
fprintf(output_filep, "ERROR: Array of pointers for 2-D array could not be allocated\n");
free(boardpp);
return 1;
}
for(i = 0; i < num_rows; i++)
{
boardpp[i] = (int*)malloc(sizeof(int) * num_cols);
if(boardpp[i] == NULL)
{
fprintf(stderr, "ERROR: Array storage could not be allocated\n");
fprintf(output_filep, "ERROR: Array storage could not be allocated\n");
free(boardpp[i]);
return 1;
}
}
// this block of code is causing error with printing to output file
if(init_type == 1)
{
InitBoardRand(boardpp, num_rows, num_cols, initSeed);
}
else if(init_type == 2)
{
InitBoardChecker(boardpp, num_rows, num_cols);
}
else if(init_type == 3)
{
InitBoardAllWhite(boardpp, num_rows, num_cols);
}
In this block of code toward the middle where it says "fprintf(output_filep, "testing opening output, success\n");", it prints the message to the output file no problem, as long as none of the three functions in the last if-else block get called. If I comment out the three if-else statements at the end, nothing gets printed to output_filep but the program still runs, it just doesn’t do anything. Very baffled rn because how can calling a function AFTER the print statement cause "fprint" to not work???
I tried commenting out the last three if-else statements. I have tried defining the functions before AND after main(). From what I’ve noticed, as long as the functions don’t get called, the fprintf statement works fine.
>Solution :
boardpp = (int**) malloc(sizeof(int*) * num_rows);