Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

File Operations (open, close, read, write)

File operations in C allow you to interact with files on your system, such as creating, reading from, writing to, and closing files. C provides several functions in the <stdio.h> library to manage these operations. Here’s an overview of common file operations:

  1. fopen – Open a file.
  2. fclose – Close a file.
  3. fread – Read from a file.
  4. fwrite – Write to a file.

1. fopen – Open a File

The fopen function opens a file with a specified mode. It returns a pointer to FILE, which is used to manage the file operations.

  • Modes:
    • "r" – Open for reading (file must exist).
    • "w" – Open for writing (creates a new file or truncates an existing one).
    • "a" – Open for appending (creates a new file if it doesn’t exist).
    • "r+" – Open for reading and writing.
    • "w+" – Open for reading and writing (overwrites the file if it exists).
    • "a+" – Open for reading and appending.

Example of fopen:

#include <stdio.h>

int main() {
    FILE *file;

    // Open file in write mode
    file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }

    printf("File opened successfully.\n");

    fclose(file);  // Closing the file
    return 0;
}

Explanation:

  • fopen("example.txt", "w") opens example.txt in write mode. If it doesn’t exist, it will be created.
  • fclose(file) closes the file after operations are done.

2. fclose – Close a File

The fclose function closes an open file and releases any resources associated with it. It’s important to close files after use to avoid memory leaks or corruption.

Example of fclose:

fclose(file);  // Close the file after operations

3. fwrite – Write to a File

The fwrite function writes data to a file. It’s often used to write binary data, but it can be used for text as well.

Example of fwrite:

#include <stdio.h>

int main() {
    FILE *file;
    char data[] = "Hello, world!";

    // Open file in write mode
    file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }

    // Write data to file
    fwrite(data, sizeof(char), sizeof(data), file);

    printf("Data written to file.\n");

    fclose(file);  // Close the file
    return 0;
}

Explanation:

  • fwrite(data, sizeof(char), sizeof(data), file); writes the contents of data to the file.
  • The parameters specify the data pointer, size of each element, number of elements, and the file pointer.

4. fread – Read from a File

The fread function reads data from a file and stores it in a buffer.

Example of fread:

#include <stdio.h>

int main() {
    FILE *file;
    char buffer[50];

    // Open file in read mode
    file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }

    // Read data from file
    fread(buffer, sizeof(char), sizeof(buffer) - 1, file);
    buffer[49] = '\0';  // Null-terminate the buffer

    printf("Data read from file: %s\n", buffer);

    fclose(file);  // Close the file
    return 0;
}

Explanation:

  • fread(buffer, sizeof(char), sizeof(buffer) - 1, file); reads data into the buffer.
  • sizeof(buffer) - 1 ensures room for a null terminator in the buffer.

Summary of File Operations

  • fopen: Opens a file and returns a FILE pointer.
  • fclose: Closes an open file.
  • fwrite: Writes data to a file.
  • fread: Reads data from a file.

    Leave a Reply

    Your email address will not be published.

    Need Help?