tags:

views:

213

answers:

3

Hi

I am getting a malloc error with bus error on using the fprintf statements in C++ (code below). Any pointers on what could be going wrong? Note absAmb and dModel both have valid values. Thanks.

FILE *fPtr;
char fName[100];

sprintf(fName, "Info.dat", block);
if ( (fPtr = fopen(fName,"w")) == NULL )
{  
    return( FALSE );
}

int absAmb = rint(fda[0]/prf[0]);

fprintf(fPtr, "  %d", absAmb); //ERROR LINE
fprintf(fPtr, "  %d", dModel);
fclose(fPtr);
+2  A: 
fprintf(f, "  %d", absAmb); //ERROR LINE

You use wrong variable in fprintf

fprintf(fPtr, "  %d", absAmb); // <--- fPtr
Nick D
A: 

You've declared your file pointer variable "fPtr", but you are trying to write to "f". Change your fprintf() call to use fPtr as the first parameter.

Allan
+1  A: 

What is 'f'? You stored the results of fopen into fPtr but then do a fprintf to f.

Brad