Why would a file fail to open/close

5.7k views Asked by At

I am trying to include statements that instruct the user a little bit more about why a file failed to open or close. What are some possible situations where a file would fail to open in write mode and what about failing to close?

FILE *fp;

if(!(fp = fopen("testing", "w")))
    {
    fprintf(stderr, "\nError %d: Loading from \"testing\" file failed: %s\n",
    errno, strerror(errno));
    printf("Add additional explanations here\n");
    }

fclose(fp);
5

There are 5 answers

0
nesderl On

The error you can get from trying to open a file in write mode are OS specific. But it's basically that the owner or the user running the program don't have the rights to write to the file.

Same for the fclose, it's os specific but as it automatically execute a fflush, that is an operation that can fail when you don't have enough space on your disk for example.

1
lwi On

The fopen function is a system call. So the reason of a failure is heavily depending on your OS.

You can check which error happend by calling depending on your OS (getLastError() on Windows and errno on Linux). In addition, there is normally a header file on POSIX systems that maps the number to a reason, called errno.h.

Here as a example the linux man page regarding errno:

http://man7.org/linux/man-pages/man3/errno.3.html

0
alk On

If fopen() fails and errno had been set EACCES it might be interessing to know for the user about the actual rights of the file in question, so in this case a call to stat() pulling those rights and displaying them to the user might be appropriate.

Also always log the file's name.

0
lorenzog On

First of all, you could try using perror (see here).

Also, the list of errors of fopen is listed on its POSIX page: http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html.

An extract of the above:

[EACCES] Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by mode are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.

[EINTR] A signal was caught during fopen().

[EISDIR] The named file is a directory and mode requires write access.

[ELOOP] A loop exists in symbolic links encountered during resolution of the path argument.

In the end it all boils down if you want to print an error right away, or perhaps return it to a higher level to be parsed. Your user might not care/know what's a loop in symbolic links.

1
Kailas On

Windows depends on the file extension to associate a data file with an application used to open it, which it calls the file association. An application associated with a file extension is called its default program. When you double-click a data file and Windows examines the file extension and doesn't know which application to call on to open the file, it displays an error message. The solution to this problem is to change the file association for the data file's file extension.