All about Files in C Programming

Danny Padron
5 min readMar 12, 2021

--

When you have a program that needs to store data on permanent storage instead of data that isn’t persisted. In C a file can store non-volatile data and is it is usually stored on a disk or a SSD. This way even when the computer or program is turned off or closed the data will still persist on the next start up. The C programming language sees files as a continuous sequence of bytes, and each byte can be read individually. Each file has three parts to it, 1) The beginning, 2) The current position in the file and 3) The end of the file (EOF). The current position in the file is where any file action is going to take place, but the current position can be moved at any point in the file. There are two ways to write data that represents a file, it can be a text file or a binary file. You can write any data to a file that you want, once the file has been written it really just consists of a series of individual bytes. When you want to read a file you need to understand the format that the file was written in. All files on a disk have a name and depending on your OS there are certain rules for naming the files. A C program references a file through a file pointer, this file pointer points to a struct of type FILE that represents a stream(an abstract representation of any external source or destination for data). The file pointer contains information about the file, whether you want to read or write, the address in memory and a pointer to the current position in the file. All these are set via input/output file operations. Files can keep non-volatile data but there are certain functions that need to be used to access these files and to be able to write, read, create or update these files.

The first thing when it comes to using files in your C program is understanding how to open the file within the program and also how to close the file when the program is done using it. First we will start with being able to open up a file in a C program to be able to access the data inside. We use the fopen() function that is defined in <stdio.h> to create a file pointer for the specific external file. This is what the syntax would look like:

FILE *filePointer;filePointer = fopen("myfile.txt", "w");

The first argument in the fopen() a string that is the name of the external file that you want to access, the second argument specifies what we want to do with the file. If the fopen() function is successful it will return a pointer of type FILE* that can be used to reference the file in other input/output operations. If fopen() is not successful then it will return NULL . When you have finished with a file inside the program you have to tell the program to close the file. The function to do this is fclose() and it takes one argument of the file pointer that is used for the file, so it would look like this :

fclose(filePointer);
filePointer = NULL;

The result of this function is that the pointer and the file are no longer connected and filePointer will no longer be able to be used to access the file. It is always good practice to close a file as soon as is not being used because it protects against output data loss and also your program will run slower the more files it keeps open.

Having access to open and close files within the program and use the data within those files is great for really large scale programs to not have to manually enter the data you are working with every time the program starts up. Which of course there are certain ways that writing new data to the files or reading data from files and even deleting files within the program all have their own set of functions and syntax. When we want to write data to an existing file we first have to open the file using our fopen() function and then making sure that the second argument to the function is "w" or "w+" . This just lets the program know what we will be doing with the file, using "w" means you can only write to the file and can not read it and using "w+" lets you both write and read the file. Using "w+" does truncate the file length to 0 if exists or will create the file if it does not exist, using "w+" will overwrite everything in the current file with the new data the program will input to it. If you don’t want to have the previous data overwritten with the new data you can open the file in “append” mode like so:

filePointer = fopen("myfile.txt", "a"); 

Specifying the mode as "a" will append all the newly written data to the end of the file without overwriting the previous data. In append mode all write operations will be added to the end of the file and you will not be able to update any of the previous data in append mode. If you only needed to read the data in the file you would use :

filePointer = fopen("myfile.txt", "r");

Utilizing "r" as the mode it will position the file at the very beginning of the data. If you use the reading mode and the file entered does not exist fopen() will return NULL .

Other things that can be done with the C program to files is that they can be renamed or even deleted all together from the program. There are two built in functions with <stdio.h> that make it very simple to rename or delete files. First if you wanted to rename a file you would use the rename() function that <stdio.h> provides. When the function is used the file must not be opened or the rename will fail, if the rename is successful it will return 0 .

int rename(const char *oldname, const char *newname);

The function to delete a file is just a simple as renaming it, the function provided to delete is simply remove() . This function only takes in one argument which is a string of the file name. Again the file must not be opened when performing this function or it will fail. These are just the basic methods of being able to read and write data from external files so that the data within your C program can have persistent data. There are numerous other functions that need to be used when maneuvering through files either for reading or writing, there are functions to determine where specifically in a file you currently are, functions that need to be used to write certain types of data to files.

--

--

Danny Padron
Danny Padron

Written by Danny Padron

Full stack web developer with experience in Ruby on Rails, JavaScript, React and Redux and learning more!

No responses yet