File Handling in C
File:
- A file is a collection of data stored in one unit, identified by filename.
- The primary purpose of a file is to keep a record of data, where a record is a group of related fields.
- Each file ends with an end of file (EOF) at a specified byte number, recorded in FILE structure.
- A file must first be opened properly before it can be accessed for reading or writing. When a file is opened, an object (buffer) is created and a file is associated with the buffer.
- In general, a file is a collection of related records such as student’s name, date of birth, address, marks, phone number, etc.
- A file is nothing but a source of storing information permanently in the form of a sequence of bytes on a disk.
Buffer:
- When the computer reads, the data move from the external device to memory, and when it writes, the data move from memory to the external device. This data movement often uses a special work area known as a buffer.
- A buffer is a temporary storage area that holds data while they are being transferred to or from memory.
- The primary purpose of a buffer is to synchronize the physical devices with a program’s need.
File Handling:
- File handling is the process of storing data in the form of input or output produced by running C programs in a data file for future reference and analysis. And, we can extract/fetch data from a file to work with it in the program.
- File handling provides a mechanism to store the output of a program in a file and to perform various operations on it.
- File handling concept provides various operations like creating a file, opening a file, reading a file, or manipulating data inside a file, etc.
Why file handling?
- The data stored in the various parts of a program will be lost once the program is terminated because they are stored in the Random Access Memory (RAM) which is volatile memory.
- So, if we want to store that data (input/output) used in the program permanently inside the secondary storage device so that we can access these data from there whenever it is needed, file handling concept is important.
Block diagram of file accessing using various functions:
Types of files:
- Text file:
- These are the simplest files a user can create when dealing with file handling in C.
- It is created with a .txt extension using any simple text editor.
- A text file stores information in the form of ASCII characters internally, but when you open the text file, you would find the content of the text readable to humans.
- Text files consume large storage space.
- Example: new.txt, file.c etc.
- Binary file:
- A binary file stores information in the form of the binary number system (0’s and 1’s) and hence occupies less storage space.
- In simple words, it stores information in the same way as the information is held in computer memory. Therefore, it proves to be much easier to access.
- Example: file.dat, photo.jpg, one.png, song.mp3, file.exe etc.
Difference between Text File and Binary File:
Text File | Binary File |
---|---|
Output Formatted Text. | Output in 0 and 1 binary format. |
EOF (End of File), \n (New Line) are used. | Binary mode not use such character. |
fgetc( ), fputc( ), fprintf( ), fscanf( ), fgets( ), fputs( ) are used. | fread( ) and fwrite( ) functions are commonly used. |
Numbers are stored as string. | Numbers are stored as int, float etc. |
Takes more access time. | Takes less access time. |
It occupies more memory than binary file. | It occupies less memory than text file. |
Operations on File:
- Creating a new File
- Opening an existing File
- Reading from a File
- Write to a File
- Moving to a specific location in a file
- Closing a File
File Operation Modes:
Mode | Description |
---|---|
"w" (write) | If the file doesn't exist then this mode creates a new file for writing, and if the file already exists then the previous data is erased and the new data entered is written to the file. |
"r" (read) | This mode is used for opening an existing file for reading purpose only. The file to be opened must exist and the previous data of the file is not erased. |
"a"(append) | If the file doesn't exist then this mode creates a new file and if the file already exists then the new data entered is appended at the end of existing data. In this mode, the data existing in the file is not erased as in “w” mode. |
"w+"(write + read) | This mode is same as “w” mode but in this mode we can also read and modify the data. If the file doesn't exist then a new file is created and if the file exists then the previous data is erased. |
"r+"(read + write) | This mode is same as “r” mode but in this mode we can also write and modify existing data. The file to be opened must exist and the previous data of the file is not erased. Since we can add new data and modify existing data so this mode is also called update mode. |
"a+"(append + read) | This mode is same as the “a” mode but in this mode we can also read the data stored in the file. If the file doesn't exist, a new file is created and if the file already exists then new data is appended at the end of existing data. We cannot modify existing data in this mode. |
File Operation Modes (Continued):
Mode | Description |
---|---|
"wb" | Binary file opened in write mode. |
"ab" | Binary file opened in append mode. |
"rb" | Binary file opened in read mode. |
"wb+" | Create a binary file for read/write. |
"rb+" | Open a binary file for read/write. |
"ab+" | Append a binary file for read/write. |
Text mode vs Binary mode:
- Text mode: In text mode, every digit or text is stored as a character. When reading the content back, conversion is required from character to appropriate format and takes lots of space. Character I/O, string I/O, and formatted I/O use text mode. For example, if 3.14159 is stored in character mode, file size would be 8 bytes (counts each character including decimal and EOF).
- Binary mode: In binary mode, every digit or text is stored in binary format. When reading the content back, no conversion is necessary and takes little space. fread() and fwrite() are used in binary mode. For example, if 3.14159 is stored in binary mode, file size would be 4 bytes.
Opening a file:
- A file must be opened before any I/O operations are performed on that file. The process of establishing a connection between the program and file is called opening the file.
- A structure named FILE is defined in the file
<stdio.h>
that contains all information about the file like name, status, buffer size, current position, end of file status, etc.
Closing a file:
- The file that was opened using fopen() function must be closed when no more operations are to be performed on it.
- After closing the file, the connection between file and program is broken.
- On closing the file, all the buffers associated with it are flushed, i.e. all the data that is in the buffer is written to the file.
End of file (EOF):
- The file reading function needs to know the end of file so that they can stop reading.
- When the end of file is reached, the operating system sends an end-of-file signal to the program.
- When the program receives this signal, the file reading function returns EOF, which is a constant defined in the file
<stdio.h>
and its value is -1.
File Processing Techniques:
- Sequential file processing: In this type of files data is kept in sequential order. If we want to read the last record of the file, we need to read all records before that record so it takes more time.
- Random file processing: In this type of files data can be read and modified randomly. If we want to read the last record we can read it directly. It takes less time when compared to sequential file.
Input/output Functions:
The functions used for file input/output are:
- Character I/O:
fputc()
: Writes a character to the specified file at the current file position and then increments the file position pointer.fgetc()
: Reads a single character from a given file and increments the file pointer.
- Integer I/O:
putw()
: Writes an integer value to the file pointed to by file pointer.getw()
: Returns the integer value from the file associated with the file pointer.
- String I/O:
fputs()
: Writes the null-terminated string pointed by a character pointer to a file.fgets()
: Reads characters from a file and stores them in the string pointed by a character pointer.
Formatted Input/output Functions:
The formatted input/output functions include:
fprintf()
: Similar to theprintf()
function but writes formatted data into the file instead of the standard output.fscanf()
: Similar to thescanf()
function but reads data from the file instead of standard input.
Random access in a file:
Random access means you can move to any part of a file and read or write data from it without having to read through the entire file. This technique can be performed using the following functions:
fseek()
: Used to move the file position to a desired location within a file.ftell()
: Used to get the current position of the file pointer.rewind()
: Used to set the file pointer at the beginning of the file.
fseek() Function:
This function is used to move the file position to a desired location within a file.
- Syntax:
int fseek(FILE *fp, long int offset, int position);
- Parameters:
fp
: A file pointer of type FILE which identifies the file.offset
: Specifies the number of positions (bytes) to be moved from the location specified by position. The offset may be positive (move forward) or negative (move backwards).position
: Specifies the value of the position. It can be:0
(SEEK_SET): Beginning of the file.1
(SEEK_CUR): Current position.2
(SEEK_END): End of the file.
- Examples of
fseek()
:fseek(fp, n, SEEK_CUR)
: Set file position ahead from the current position byn
bytes.fseek(fp, -n, SEEK_CUR)
: Set file position back from the current position byn
bytes.fseek(fp, 0, SEEK_END)
: Set file position to the end of the file.fseek(fp, 0, SEEK_SET)
: Set file position to the beginning of the file.
ftell() Function:
This function is used to get the current position of the file pointer.
- Syntax:
int ftell(FILE *fp);
- Parameters:
fp
: The file pointer which identifies the file.
rewind() Function:
This function is used to set the file pointer at the beginning of the file.
- Syntax:
void rewind(FILE *fp);
- Parameters:
fp
: The file pointer which identifies the file.
Deleting a File:
To delete a file, the remove()
function is used.
- Syntax:
remove("file_name");
- Example:
int main()
{
remove("std.txt");
printf("Your file is successfully deleted.");
getch();
return 0;
}
Renaming a File:
To rename a file, the rename()
function is used.
- Syntax:
rename("existing_file_name", "new_file_name");
- Example:
int main()
{
rename("std.txt", "std1.txt");
printf("Your file is renamed successfully.");
getch();
return 0;
}
Error Handling in Files:
It is possible that errors may occur during I/O operations on a file. To handle these errors, C provides error handling functions:
feof()
: Used to detect the end of file (EOF) character in the file.ferror()
: Used to detect errors while reading from or writing to the file.
feof() Function:
- Syntax:
int feof(FILE *fp);
- Returns: Non-zero if the file pointer is on EOF, otherwise returns 0.
ferror() Function:
- Syntax:
int ferror(FILE *fp);
- Returns: Non-zero if an error occurs, otherwise returns 0.