File handling

File handling refers to the operations performed on files, such as creating, reading, writing, and manipulating data stored in files. It allows programs to interact with external files on a computer's storage system. 

File handling is a fundamental aspect of many programming languages and is commonly used for tasks like data storage, configuration management, logging, and more.

File operations (opening, reading, writing, closing)

Opening and Closing Files:

To work with a file, you need to open it. Opening a file establishes a connection between the file on disk and your program. You can specify the file's name, location, and the type of operation you want to perform (read, write, append, etc.). Once you're done working with the file, you should close it to release system resources.

Reading from Files:

Reading from a file involves retrieving data from the file and loading it into your program. The specific method depends on the programming language and the file format. You can read the entire file at once or read it line by line.

Writing to Files:

Writing to a file involves storing data from your program into the file. It allows you to save data persistently for later use. You can write data in different formats, such as plain text, binary, or structured formats like JSON or XML.

Text and binary files

Text Files:

Text files contain human-readable text data, typically encoded using ASCII or UTF-8. They store characters and can be opened and edited using text editors. Text file input/output functions treat the data as strings.

Binary Files:

Binary files contain data in a non-text format. They can store any type of data, including integers, floating-point numbers, structures, or arrays. Binary file input/output functions treat the data as binary data i.e 0's & 1's.

Common File Input/Output Functions

a. Opening a File:

fopen(filename, mode): Opens a file with the specified filename and mode (read, write, append, etc.) and returns a file pointer.


b. Reading from a File (Text and Binary):

fscanf(file, format, ...): Reads formatted data from a text file.

fgets(buffer, size, file): Reads a line from a text file into a character buffer.

fread(ptr, size, count, file): Reads binary data from a file into a memory location.


c. Writing to a File (Text and Binary):

fprintf(file, format, ...): Writes formatted data to a text file.

fputs(string, file): Writes a string to a text file.

fwrite(ptr, size, count, file): Writes binary data from a memory location to a file.


d. File Positioning:

fseek(file, offset, origin): Sets the file position indicator at a specified offset.

ftell(file): Returns the current file position indicator.


e. Error Handling:

feof(file): Checks if the end-of-file indicator for the file has been set.

ferror(file): Checks if an error occurred during file input/output operations.


f. Closing a File:

fclose(file): Closes the specified file.

Error handling

Error handling in file operations is crucial to handle unexpected situations and ensure the reliability of your program. In C, you can use various techniques and functions to handle errors that may occur during file handling.

Checking File Opening Errors:

When opening a file using the fopen() function, it's important to check if the file was successfully opened. The fopen() function returns a NULL pointer if it fails to open the file. You can check for this NULL value to determine if the file opening was successful. 

Checking End-of-File (EOF) and Error Indicators:

The feof() function checks if the end-of-file indicator has been set for a file. It returns a non-zero value if the end-of-file has been reached. Similarly, the ferror() function checks if any error occurred during file input/output operations. It returns a non-zero value if an error occurred. You can use these functions to handle errors or unexpected conditions in your code. 

Handling File I/O Errors:

You can use the perror() function to print a descriptive error message associated with the last error encountered during file input/output operations. It takes a string parameter that serves as an error message prefix. 

Cleaning Up and Closing Files:

It's essential to close the files properly after you finish working with them. Use the fclose() function to close the file. Closing the file releases system resources and ensures that any pending data is written to the file.