Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling a function causes error with "fprintf"

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

boardpp = (int**) malloc(sizeof(int*) * num_rows);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading