- Creation of the new file
- Opening an existing file
- Reading from the file
- Writing to the file
- Deleting the file
Functions for file handling
There exist numerous functions within the C library for initiating, accessing, modifying, exploring, and terminating file operations. Below is a compilation of file-related functions:
| No. | Function | Description |
|---|---|---|
1 |
fopen() | opens new or existing file |
2 |
fprintf() | write data into the file |
3 |
fscanf() | reads data from the file |
4 |
fputc() | writes a character into the file |
5 |
fgetc() | reads a character from file |
6 |
fclose() | closes the file |
7 |
fseek() | sets the file pointer to given position |
8 |
fputw() | writes an integer to file |
9 |
fgetw() | reads an integer from file |
10 |
ftell() | returns current position |
11 |
rewind() | sets the file pointer to the beginning of the file |
Opening File: fopen
We need to initiate access to a file before performing any read, write, or update operations on it. The fopen function is employed for this purpose. The format of the fopen function is illustrated as follows.
FILE *fopen( const char * filename, const char * mode );
The fopen function takes in two arguments:
- The name of the file (a string). If the file is located in a specific directory, the path must be specified. For instance, a file name could be "c://somefolder/somefile.ext".
- The specific mode in which the file should be opened. This parameter is also a string.
We have the option to utilize one of the following modes within the fopen function.
| Mode | Description |
|---|---|
r |
opens a text file in read mode |
w |
opens a text file in write mode |
a |
opens a text file in append mode |
r+ |
opens a text file in read and write mode |
w+ |
opens a text file in read and write mode |
a+ |
opens a text file in read and write mode |
rb |
opens a binary file in read mode |
wb |
opens a binary file in write mode |
ab |
opens a binary file in append mode |
rb+ |
opens a binary file in read and write mode |
wb+ |
opens a binary file in read and write mode |
ab+ |
opens a binary file in read and write mode |
The fopen function works in the following way.
- Firstly, It searches the file to be opened.
- Then, it loads the file from the disk and place it into the buffer. The buffer is used to provide efficiency for the read operations.
- It sets up a character pointer which points to the first character of the file.
Consider this illustration that initiates a file in a write mode.
Example
#include<stdio.h>
void main( )
{
FILE *fp ;
char ch ;
fp = fopen("file_handle.c","r") ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
printf("%c",ch) ;
}
fclose (fp ) ;
}
Output
The content of the file will be printed.
#include;
void main( )
{
FILE *fp; // file pointer
char ch;
fp = fopen("file_handle.c","r");
while ( 1 )
{
ch = fgetc ( fp ); //Each character of the file is read and stored in the character file.
if ( ch == EOF )
break;
printf("%c",ch);
}
fclose (fp );
}
Closing File: fclose
The fclose function is employed to terminate access to a file. It is essential to close the file once all necessary operations have been completed. Below is the syntax for the fclose function:
int fclose( FILE *fp );