Dear All, I have implemented flash file system on LPC1768. Creating, writing and reading files successfully. If i try to open a file in read mode, a file that doesnt exist, then the controller goes into hardfault which is forced from precise bus fault error.
So, to stop my program from trying to read a file that doesnt exist, i used ferror as shown in the code below. But i m still getting an hardfault.
parse_proto_name(LOAD_PROTOCOL); //Get Protocol Name FileName = (char*)gProtocolName; fProtoName = fopen (FileName,"r"); //Open the File for reading if(ferror(fProtoName)) return; //Return if an error occured while trying to read a file. while(!feof(fProtoName)) { temp = fread(&gTx0Buf[dtcntr], sizeof(char), 640, fProtoName);//Copy the file contents into TxBuf } fclose(fProtoName);
Details of variables
// Global Variables uint8_t gProtocolName[32]={0}; //To store the name of the protocol received from the display FILE *fProtoName; //Local Variables uint8_t dtcntr;//data counter
Am i expecting too much from ferror & foef?
Why bother with ferror() if you didn't even check whether fopen() returned NULL, first?
Given it almost certainly did return NULL here, you're calling ferror(NULL) here. Hardfault is well within expected behaviour if you do that.
Thank you. changed the code to
parse_proto_name(LOAD_PROTOCOL); //Get Protocol Name FileName = (char*)gProtocolName; fProtoName = fopen (FileName,"r"); //Open the File for reading if(fProtoName == NUL) //Return if the file doesnot exist return; while(!feof(fProtoName)) { temp = fread(&gTx0Buf[dtcntr], sizeof(char), 640, fProtoName); } fclose(fProtoName);
Is there any better way ?
if(fProtoName == NUL)
That's supposed to read NULL. NUL is similar, but not really the same thing.
That was a mistake. Thanks for pointing it out. I have corrected it.