C - File Handling

File handling in C allows you to work with files, including creating new files, writing data to files, and reading data from files. Here's an explanation of file handling operations in C:

Create a File:

To create a new file in C, you need to use the fopen() function, which takes two parameters: the file name and the mode. The mode specifies the purpose of opening the file, such as reading, writing, or both. The fopen() function returns a file pointer that you can use to perform further operations on the file.

#include <stdio.h>
int main() {
    FILE *filePtr;  // File pointer
    // Open a file in write mode
    filePtr = fopen("myfile.txt", "w");
    if (filePtr == NULL) {
        printf("Unable to create the file.\n");
        return 1;
    }
    printf("File created successfully.\n");
    // Close the file
    fclose(filePtr);
    return 0;
}

In this example, a file named "myfile.txt" is created using fopen() in write mode ("w"). If the file creation is successful, it prints a success message. Finally, the file is closed using fclose().

Write to a File:

To write data to a file in C, you can use functions like fprintf() or fputc(). The fprintf() function allows you to write formatted data to a file, similar to printf(). The fputc() function is used to write individual characters to a file.

#include <stdio.h>
int main() {
    FILE *filePtr;
    int num1 = 10;
    float num2 = 3.14;
    // Open the file in write mode
    filePtr = fopen("myfile.txt", "w");
    if (filePtr == NULL) {
        printf("Unable to open the file.\n");
        return 1;
    }
    // Write data to the file using fprintf
    fprintf(filePtr, "Number 1: %d\n", num1);
    fprintf(filePtr, "Number 2: %.2f\n", num2);
    // Close the file
    fclose(filePtr);
    printf("Data written to the file successfully.\n");
    return 0;
}

In this example, two variables (num1 and num2) are written to the file "myfile.txt" using fprintf(). The formatted data is written to the file using placeholders (%d for integers, %.2f for floats). Finally, the file is closed.

Read a File:

To read data from a file in C, you can use functions like fscanf() or fgetc(). The fscanf() function allows you to read formatted data from a file, similar to scanf(). The fgetc() function is used to read individual characters from a file.

#include <stdio.h>
int main() {
    FILE *filePtr;
    int num1;
    float num2;
    // Open the file in read mode
    filePtr = fopen("myfile.txt", "r");
    if (filePtr == NULL) {
        printf("Unable to open the file.\n");
        return 1;
    }
    // Read data from the file using fscanf
    fscanf(filePtr, "Number 1: %d\n", &num1);
    fscanf(filePtr, "Number 2: %f\n", &num2);
    // Close the file
    fclose(filePtr);
    printf("Data read from the file:\n");
    printf("Number 1: %d\n", num1);
    printf("Number 2: %.2f\n", num2);
    return 0;
}

In this example, the file "myfile.txt" is opened in read mode ("r"). The data from the file is read using fscanf() with the appropriate format specifiers. The values are stored in the variables num1 and num2, and then printed on the console.

Remember to handle file opening errors and close the file using fclose() after performing the required operations.