C is one of the most widely used programming languages. The C language has a lot of applications that include being used to develop system software, since this powerful language is mapped efficiently with machine instructions.
A file is a collection or stream of byte data. The stream of data that is stored can be a document, a graphic image or any other collection of information, that is determined based on the structure of a file written.
The following are the two widely used types of files by the programmers.
- ASCII Text –
These are the most common type of text files used by the programmers. An ASCII text file is a collection or sequence of characters. In C language these files can only be processed in the forward direction. The programmers often use this file format with only one operation (read, write or append) at a time.
- Binary File –
The binary file is also a collection of information stored in the form of bytes. According to the design of C language, the byte and character are equivalent only. A binary file is not processed by the C language i.e., no specific processing is done on the byte when it is transferred to and fro from the file
File Handling in C:
A file in C is accessed using the FILE pointer.
There are a lot of file handling functions available in C. A few common file handling functions:
This opens a file and initializes I/O stream for the file opened.
FILE *fp =fopen(const char *path, const char *mode);
This function accepts two parameters. While the first parameter is the file path which is to be handled, the second parameter is a mode for the file which is to be opened. Fopen() has 6 different modes that can be used to open files.
The different file modes of fopen() are:
|r||File is opened for reading only|
|w||File is opened for writing only (file need not exist)|
|a||File is opened for appending (file need not exist)|
|r+||File is opened for both reading and writing. The cursor is set to the start of the opened file.|
|w+||File is opened for both reading and writing, and data is overwritten.|
|a+||File is opened for both reading and writing, and data is appended if already exists.|
NOTE – There are modes like rb, which means that file is opened for reading mode as a binary file. This can be useful when you are trying to open files other than *.txt.
The fopen() returns a pointer for the file stream as a result, but if the user tries to access a file that doesn’t exist or tries to access a read-protected file, fopen() will return 0.
The above example denotes that the user is opening a file named test.txt with the mode a+. The C compiler will search for the file in the directory where the code is written, since the path is not specified.
The fclose() function is used to close an open file. It also cleans the buffer associated to that particular file.
int fclose(FILE *stream);
The fclose() function takes a file pointer to the opened file. The return type is int, and it returns 0 if the file is closed successfully.
This function sets the file pointer indicator in the file stream.
int fseek(FILE *stream, long offset, int whence);
The fseek() function accepts 3 parameters: the first parameter is the FILE pointer, the second parameter is the offset value or the number of bytes to seek, and the third parameter accepts three defined values SEEK_SET, SEEK_CUR, or SEEK_END. While SEEK_SET denotes the start position of the stream, SEEK_CUR denotes the position of the file pointer indicator currently in the stream. SEEK_END denotes the end position of the stream.
If the file pointer indicator position is set, fseek() returns 0.
The ftell() function returns the file pointer indicator position of the given file stream.
long int ftell(FILE *stream);
The ftell() function accepts only one parameter, which is the file stream. The function returns the position of the indicator on success or the function will return -1L.
fread() and fwrite()
The fread() function is used to read the data from a file stream into the array whereas the fwrite function is used to write the data into the file stream from the array.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
fread() and fwrite() have four parameters: the first parameter is the pointer to the array in which the data that has to be stored or written, the second pointer is the size in bytes of each element that is read, the third parameter is the number of elements that are read and the fourth parameter is the file stream from which the elements are read or the elements written.
The fgetc() function is used to read a character from a file. The return type is int as fgetc() returns the ASCII value(0-255) of the character that is read from the file. If all characters are read from the file or the end of the file is reached, the fgetc() will return “EOF,” which denotes the end of file.
int fgetc (FILE *stream);
fgetc() accepts only one parameter which is the file stream.
The fputc() method is used to write one character at a time to the file.
int fputc( int c, FILE *stream );
This method accepts two parameters. The first parameter is the character value (0-255) that is written to the file and the second parameter is the file stream. The fputc() function returns the character if the character is successfully written or else it will return “EOF.”