Saturday 5 November 2016

c - fopen returns non-null pointer even though file does not exist

I have a Hex file whose contents are like below:



0x14000800
0x52100000
0xD503201F
0xD503201F
0x0030A308
0x0032D138
0x00000000
0x00000000
0x00000000
0x00000000



I need to open and read this file. Below is my code:



#include 
#include

int main(void)
{
char ch, boot_rom_golden[16];
FILE *myFile = NULL;

myFile = fopen("/prj/vlsi/tests/boot_rom_fail/src/apps_proc0/sm6140_rom.a52100000_ROM_FULL.hex", "r");

if (myFile == NULL) {
printf("Error Reading File\n");
exit(0);
}

while ((ch = fgetc(myFile)) != EOF) {
printf("%x \n", ch);
}


I have two questions:




  1. My understanding is if the file does not exist in the above mentioned path, then fopen should return a NULL. Observation is : even if the file does not exist in the above path (/prj/vlsi/....) , fopen is returning some value and then it goes to while loop trying to print the content. Why is this happening? My main.c and the hex file are residing in the same path. But still I tried giving the complete path which also gave the same results (i.e. even if file does not exist it is returning a non zero pointer value)


  2. When the code executes while loop, it prints "FF" indefinitely. It should be because of reason stated above.




Please help to know the issue and how to debug these kind of issues ?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...