In our previous tutorials, we have discussed on Files and all different types of modes and what is their meaning for Files in C programming language. Now, we will discuss some of them programmatically and become familiar with the different file operations and how they are performed. The various file operations are:
- Creation of a new file
- Opening or accessing an existing file
- Reading from a file
- Writing to a file
- Seeking in a file or moving to a specific location
- Closing a file
Before we start with reading or writing to a file we must first learn about the buffer.
In order to read or write to a file, we must first load the file to a buffer from the disk. This happens automatically once a file is opened in read or write mode. Now, the question that arises here is why do we need a buffer memory between the disk and the program to read or write or perform other operations to a file? The answer lies in the inefficiency of the process of accessing the disk every single time a character is written to a disk or read from the disk. It takes time for the system to adjust the read/write head correctly every time the file is accessed. So, we use a buffer memory which copies the file from the physical disk and then all the read/write operations are performed on the copy of the file inside the buffer. When the file is closed the changes made in the file are transferred to the file on the disk.
It also prevents us from the scenario(if in case the file is not copied to the buffer and the operations are performed on physical memory) where multiple read and write operations are performed on the same file at the same time. In this case, it might change the data for the other user dynamically and can cause data inconsistency and data curroption.
Creating a file
The file modes play a very important part in creating or opening a file. In order to create a new file, we use the file mode “w”. The syntax for creating a new file is:
As we already know that if a file with the same name already exists then the data is overwritten otherwise a new file is created.
Opening a File
In order to read or write to a file, we must first open the file using the function fopen(). We open the file in “r” or read mode which allows us to read the contents of the file. When we open the file in ‘read mode’, the file is first searched for in the disk and then loaded into the buffer. A char pointer is set up which shall point to the first character of the buffer. The syntax for opening a file in ‘read mode’ is:
A file can be opened in other modes as well depending upon the programmer’s requirement. Read more here for understanding different file modes.
Reading a File
When we open a file we assign a pointer variable to the File which points to the first character. The file is opened in the ‘read mode’ as shown in the above example. Now, to read the contents of the file we have another function fgetc() which fetches or reads the first character from the current position of the pointer and stores it in another variable and shifts the pointer to the next character.
In this way, the characters in the file are read one by one. The function fgetc() is used inside a while loop which runs indefinitely.But, running a loop indefinitely is not feasible so we have the EOF or End of file macro defined in the header file ‘stdio.h’. The function fgetc() returns an EOF macro once all the characters inside the file are read. We have another function
We have another function getc() which produces the same result as fgetc(). Here is the syntax of how we read a file using the fgetc() function:
Note: Once we open the file, we don’t need to refer to it by its name. We use the file pointer for that purpose.
Writing to a file
In order to write to a file, we need to open the file in the write mode, i.e., using “w” mode.
Now that we have opened the file in write mode we use the function fputc() to write to the file. One can write to the file using a while loop and once all the characters are exhausted the loop exits. The fputc() function is used in the following manner:
char ch = 'a'; //ch is the variable that contains the character to be written to the file.
Closing a File
Once we are done with the reading and writing or other file operation it is important to close the file. We use the function fclose() to close the file. The same file pointer is used to close the file and not the file name.
fclose(fp);//closes the file which was opened in read mode.
fclose(fw);//closes the file that was opened in write mode.
Note :- Once we close the file, the function getc() can no longer read the contents of the file unless the file is reopened. Also, the data written to the file will be written to the file on the disk and the buffer will be eliminated from the memory. Consider a situation where the buffer gets full before closing the file and we still have data to write to the file. In this case, the buffer will copy all the data to the file present on the disk once it becomes full. This occurs automatically with the help of library functions.
Program to Read the contents of a File and Write a string into another file
FILE *fp,*fw;//we create a file read pointer and a file write pointer
fp=fopen("E:\\Doc1.txt","r");//opens the txt file present in E:// drive in read mode
/*We read the contents of the file Doc1.txt and print it*/
ch=fgetc(fp);//reading the content of the file
if(ch==EOF)//checks whether the loop reached the end of file
fw=fopen("E:\\Doc2.txt","w");//opens the file in write mode. Since there's no file with the name Doc2.txt, a new file is created.
/*copying the string in the array ch1 to the file Doc2*/
fclose(fp);//close the file Doc1.txt
fclose(fw);//close the file Doc2.txt
The file(Doc2.txt) created by the program in the Disk is shown in the pictures below:
In the image below we see both the existing file Doc1.txt and the new file Doc2.txt:
Learning never exhausts the mind.So, do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Share and subscribe.
Keep Coding!! Happy Coding!!