Basics of File Handling in C Programming

File Handling in C programming involve various tasks related to handling files, such as creating, opening, reading, writing, and closing files. These operations are performed using functions provided by the C Standard Library, such as fopen, fclose, fread, and fwrite. Here are details on these operations with examples:

Opening a File (fopen)

To open a file, we use the fopen() function. This function takes two arguments: the name of the file to open and the mode in which to open it. The mode can be one of the following:

  1. r: Open the file for reading.
  2. w: Open the file for writing. If the file does not exist, it will be created. If the file already exists, it will be truncated.
  3. a: Open the file for appending. If the file does not exist, it will be created. If the file already exists, the new data will be appended to the end of the file.

Example of opening a file for writing:

FILE* file = fopen("example.txt", "w"); // Opens or creates "example.txt" for writing if (file == NULL) { perror("File opening failed"); return 1; }

Closing a File (fclose)

The fclose function is used to close a file when you're done with it. This is important for releasing system resources and ensuring data is written if it's a write operation.

Example of closing a file:

if (fclose(file) != 0) { perror("File closing failed"); return 1; }
Full Source:
#include <stdio.h> int main() { FILE* file; // Open a file for writing (creates a new file or overwrites an existing one) file = fopen("output.txt", "w"); // Check if the file was opened successfully if (file == NULL) { perror("File opening failed"); return 1; // Exit with an error code } // Write data to the file fprintf(file, "Hello, World!\n"); // Close the file when done fclose(file); return 0; // Exit with a success code }

Reading from a File (fread)

The fread function is used to read binary data from a file. It takes the destination memory location, the size of each element to read, the number of elements to read, and the file pointer.

Example of reading binary data:

int data[5]; fread(data, sizeof(int), 5, file); // Reads 5 integers from the file
Full Source:
#include <stdio.h> int main() { FILE* file; char buffer[100]; // A buffer to store the read data // Open a file for reading file = fopen("input.txt", "r"); // Check if the file was opened successfully if (file == NULL) { perror("File opening failed"); return 1; // Exit with an error code } // Read data from the file while (fgets(buffer, sizeof(buffer), file) != NULL) { // Process or print the data here printf("%s", buffer); } // Close the file when done fclose(file); return 0; // Exit with a success code }

Writing to a File (fwrite)

The fwrite function is used to write binary data to a file. It takes the source memory location, the size of each element to write, the number of elements to write, and the file pointer.

Example of writing binary data:

int data[5] = {1, 2, 3, 4, 5}; fwrite(data, sizeof(int), 5, file); // Writes 5 integers to the file

Checking for the End of a File (feof)

The feof function is used to check whether the end of the file has been reached. It returns a non-zero value if the end of the file has been reached.

Example of checking for the end of a file in a loop:

while (!feof(file)) { // Read or process data here }

Other file operations

In addition to the basic operations of opening, reading, writing, and closing files, there are a number of other file operations that can be performed in C programming. These include:

  1. fseek(): This function sets the file pointer to a specific position in the file.
  2. ftell(): This function returns the current position of the file pointer in the file.
  3. rewind(): This function rewinds the file pointer to the beginning of the file.
  4. remove(): This function deletes a file.
  5. rename(): This function renames a file.

Conclusion

File operations in C are essential for handling data stored in files, and they enable the reading and writing of data to and from files in both text and binary formats. Proper error handling, as demonstrated in the examples, is essential to handle situations where file operations might fail due to issues like file not found or permission errors.