In C, what could happen if you attempt to open a file for input without checking to see if the file exists or not?

Home » Programming & Design » In C, what could happen if you attempt to open a file for input without checking to see if the file exists or not?
Programming & Design No Comments

Other answer:

John:
If you got the impression you need to check for the file's existence before opening it then you're wrong, because:

– Opening a file may fail for other reasons as well (e.g., when you don't have read permission to it).

– Even if a file exists, till you open it somebody may delete it (as the operating system is multitasking). This unfortunate situation is called "race condition".

So you just open the file, without preliminary steps, and check whether the operation was successful or not. If not, you call a system function that formats a nice error message to the user telling him the exact reason the operation failed (see @husoski's answer).

Darius:
if you got the impression you need to check for the file's existence before opening it then you're wrong, 'cause:

– opening a file may fail for other reasons as well (e…g…, when you don't have read permission to it)…

– even if a file exists, till you open it somebody may delete it (as the operating system is multitasking)… this unfortunate situation is called "race condition"…

so you just open the file, without preliminary steps, and check whether the operation was successful or not… if not, you call a system function that formats a nice error message to the user telling him the exact reason the operation failed (see @husoski's answer)…

husoski:
If a file cannot be opened, fopen() returns a NULL result instead of a FILE* pointer.

You can use the perror() function from <stdio.h> to write a message on the stderr device, or strerror() from <string.h> to return a pointer to a printable error message.

Most implementations will set the errno global to something you can test, and they will typically add values to the <errno.h> header file. On Windows, and I believe on Unix/Linux, ENOFILE means the file was not found, Include <errno.h> to get the errno global int and the error code constants defined.

Andy T:
Knowing the language, AND JUST AS IMPORTANT, PLATFORM TO RUN IT ON.

Under Linux, it is immediate Seg Fault and abort, more in tune of how it should go theoreticallly.

Under Windows, it may cause cascade FUBAR for the victimized computer.

Mac OS varies from worse than Windows to better than Linux, depending on the version.

md:
Under Windows, it may cause cascade FUBAR for the victimized computer.
Mac OS varies from worse than Windows to better than Linux, depending on the version.
mercado:
That moment person looks so low valued for a hard work, at the start, at stat.
Rasputin:
The operation will fail. If you use something like:

FILE* fp=NULL;
fp=fopen("Nofile.exists","r");

fp will be NULL.

In some cases you will get a runtime exception.

Typically I write my code like this:

FILE* fp=NULL;
int count=0;

fp=fopen("filename","r");
if(fp!=NULL)
{
count=fread(something here…….);
fclose(fp);
return count; //Anything over zero was a success
}
else
{
printf("error==%i",errno);
return -errno; //Below zero means error
}

Your code must handle the problem gracefully, or bad things can happen.

Leslie:
depends what attributes you set in the calling parameters…
bowen:
that moment person looks so low valued for a hard work, at the start, at stat…
NAZANIN:
That moment person looks so low valued for a hard work, at the start, at stat.

LEAVE A COMMENT