Basics Of File Handling In C

Posted on November 18, 2023 by Vishesh Namdev
Python C C++ Javascript Java
C Programming Language

File handling in C refers to the process of performing operations (such as reading from or writing to) on files. C provides a set of functions in the stdio.h library for file input and output. These functions allow you to open, read, write, and close files. File handling is essential for tasks like data storage, retrieval, and manipulation in programs.

Why File Handling is neccessary in C

1. Data Sharing: Files serve as a means for communication and data sharing between different programs or between different instances of the same program. Data written to a file by one program can be read by another.

2. Reusability: Data stored in files can be accessed, updated, and deleted from anywhere at any time, providing a high level of reusability. This means the information isn't confined to the program's immediate execution.

3. Portability Files can be easily transferred between systems in a computer network without losing any data. This feature minimizes the risk of coding errors and ensures the seamless exchange of information.

4. Efficiency For programs that require a large amount of input, file handling offers an efficient way to access specific parts of a file using minimal instructions. This not only saves time but also reduces the likelihood of errors in data processing.

4. Storage Capacity: Files provide the ability to store substantial amounts of data without the need to handle everything simultaneously within a program. This is particularly important when dealing with extensive datasets that may exceed the program's in-memory capacity.

Types of Files in C

1. Text Files:

1. Human-readable, store data as plain text.
2. Commonly used for configuration settings, logs, and easily understood data.
3. Examples: .txt files.
4. Functions: fprintf, fscanf, fputc, fgetc.

2. Binary Files:

1. Store data in a non-human-readable format using a sequence of bytes.
2. Efficient for complex data structures like images or compiled programs.
3. Examples: .bin files.
4. Functions: fwrite, fread.

File Modes

Before performing any file operations, you need to specify the mode in which you want to open the file. In C, the following file modes are available:

1. Read Mode ("r"): Opens the file for reading. The file must exist, otherwise an error will occur.

2. Write Mode ("w"): Opens the file for writing. If the file already exists, its contents are truncated (deleted). If the file does not exist, a new file is created.

3. Append Mode ("a"): Opens the file for appending. Data is written at the end of the file. If the file does not exist, a new file is created.

4. Read and Write Mode ("r+"): Opens the file for both reading and writing. The file must exist, otherwise an error will occur.

5. Write and Read Mode ("w+"): Opens the file for both reading and writing. If the file already exists, its contents are truncated (deleted). If the file does not exist, a new file is created.

6. Append and Read Mode ("a+"): Opens the file for both reading and appending. Data is written at the end of the file. If the file does not exist, a new file is created.

File Operations

Once a file is opened and the file pointer is set to the desired position, you can perform various file operations in C, including:

1. Reading from a file : Use the fscanf() or fgets() functions to read data from a file. With fscanf(), you can read data formatted according to a specific pattern, while fgets() reads a line of text.

2. Writing to a file: Use the fprintf() or fputs() functions to write data to a file. fprintf() allows you to write data with specific formatting, while fputs() writes a string of characters.

3. Appending to a file Use the fprintf() or fputs() functions with a file opened in append mode ("a"). The data will be written at the end of the file.

4. Closing a file: After performing the required file operations, it is essential to close the file using the fclose() function. This ensures that any pending changes are saved and the system resources associated with the file are released.