loading...

Reading and Writing Files in C: Part 3

noah11012 profile image Noah11012 ・2 min read

Reading and Writing in C (5 Part Series)

1) Reading and Writing Files in C 2) Reading and Writing Files in C: Part 2 3) Reading and Writing Files in C: Part 3 4) Reading and Writing Files in C: Part 4 5) Reading and Writing Files in C: Part 5

In the last section of the previous article, I promised that we would discuss some of the different errors that potentially could happen. While it is not necessary to learn about the different errors that could pop up, it is, however, educational and informative to know about the different errors that might come up when working with files.

Moved/Deleted

For some programs, a file is used to record the date and time of certain operations committed by the program and errors. If a user or some other program moves the file to another location or deletes it, then write operation, in theory, could fail and the program will (hopefully) recover gracefully. This usually involves recreating the file and writing all new entries that the program chooses to record. This most likely won't happen with files that are shortlived because, well the name implies, will not be around for very long for a user to move or delete. In Windows, if a file is opened by a process, you are not allowed to move or delete it. On Unix based OSes, a move or deletion of a file is considered acting upon the directory where the file resides. If open and deleted, it will remain on disk until closed.

Operations After Closing

Each operating system exposes an API for us to get resources that only it can provide. One of these resources is files. On Unix based OSes like Linux, the system call open() exists. Before allowing us to open the file, the OS must make sure that we have the permission to do so and, of course, exists on the hard drive. Once the OS has concluded that we are not in any violation of any permissions and can locate the file on the disk, it will return an ID for us to use. This ID is a numeric value and in the context of our program is assigned to an opened file. When we're done with the file, we have to let the OS know to close the file. For Unix basic OSes like Linux, close() does this. In the C standard library file I/O library, this is fclose(). Doing any operations with a file that has been closed will result in an error.

Example:

#include <stdio.h>

int main(void)
{
    FILE *file = fopen("test", "r");
    fclose(file);
    char buffer[100];
    if(fgets(buffer, 5, file) == 0)
    {
        printf("Error reading file!\n");
        return 1;
    }
    printf("%s\n", buffer);
}

Output:

~/Desktop 
 clang main.c 

~/Desktop 
 ./a.out 
Error reading file!

~/Desktop 
 

Full Storage

If the storage medium that is currently being used is at maximum storage capacity then there isn't much to do except for freeing space up by deleting files.

Faulty Hardware

Failing hard drives can and will cause file I/O operations to fail. Because of the unstable nature of a deteriorating storage medium, you may not even get an error from the OS, but is still an error nonetheless because the data that is read/written might be corrupted.

Next

For the next part, we will discuss how to save more than just text in a file.

Reading and Writing in C (5 Part Series)

1) Reading and Writing Files in C 2) Reading and Writing Files in C: Part 2 3) Reading and Writing Files in C: Part 3 4) Reading and Writing Files in C: Part 4 5) Reading and Writing Files in C: Part 5

Posted on Jan 10 '19 by:

Discussion

markdown guide