⫷⫸ What is File Handling?
File handling in the context of programming refers to the process of creating, reading, writing, and manipulating files on a storage medium such as a hard drive, SSD, or any other form of persistent storage. Files are used to store data permanently, as opposed to variables that store data temporarily while a program is running.
⫷⫸ Importance of File Handling
- Persistence: Files allow data to persist beyond the execution of a program, enabling long-term storage and retrieval.
- Data Management: Files provide a way to manage and organize data systematically.
- Large Data Sets: Files can handle large amounts of data that may not fit into memory.
- Inter-process Communication: Files can be used to exchange data between different programs.
📂Types of Files
1 Text Files:
Store data in plain text, readable by humans and text editors. These files store information in ASCII (American Standard Code for Information Interchange). Common examples include .txt
, .csv
and .log
files.
2 Binary Files:
Store data in binary format which is in the form of 0's and 1's, not directly readable by humans. It stores These files are used for storing images, executables, and other non-text data. This types of files considered to be more secure and safest option for storing data.
⫷⫸ File Operations
- Creating Files: Generating a new file on the storage medium.
- Opening Files: Accessing an existing file for reading, writing, or both.
- Reading Files: Extracting data from a file.
- Writing Files: Adding or modifying data in a file.
- Closing Files: Terminating access to an open file, ensuring all data is properly saved and resources are released.
⫷⫸ File Modes
When opening a file, different modes dictate how the file is accessed. Common modes include:
"r"
: Open for reading."w"
: Open for writing (truncates if exists)."a"
: Open for appending."r+"
: Open for reading and writing."w+"
: Open for reading and writing (truncates if exists)."a+"
: Open for reading and appending.