C Tutorial



FILE OPERATIONS IN C


📂 File Operations in C

In C, file operations allow you to perform different tasks such as opening a file, reading from it, writing to it, and closing it. Let's dive into the essential file operations in C and see how to use them effectively.

🔑 Common File Operations:
- fopen() — Open a file
- fwrite() — Write to a file
- fread() — Read from a file
- fclose() — Close a file

1. 🧑‍💻 fopen() — Opening Files

The fopen() function is used to open a file. The file can be opened in different modes such as read, write, append, etc. Here's how to open a file:

FILE *fopen(const char *filename, const char *mode);
  

The mode argument specifies how the file should be accessed. Here are some common modes:

  • "r" — Read mode (opens file for reading only).
  • "w" — Write mode (creates a new file or overwrites an existing file).
  • "a" — Append mode (opens file for appending data).

✨ Example: Using fopen()

#include 

int main() {
    FILE *file = fopen("sample.txt", "w");  // Open file for writing
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }
    fprintf(file, "This is a test.\n");  // Write to file
    fclose(file);  // Close file
    return 0;
}
  
Explanation:
This program opens a file in write mode. If the file doesn't exist, it will be created. Then, it writes a line to the file and closes it.

2. 📝 fwrite() — Writing to a File

The fwrite() function is used to write data to a file in binary format. It's commonly used for writing structures or arrays to files.

size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
  

✨ Example: Using fwrite()

#include 

int main() {
    FILE *file = fopen("numbers.dat", "wb");  // Open file for writing binary data
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    int numbers[] = {1, 2, 3, 4, 5};
    fwrite(numbers, sizeof(int), 5, file);  // Write an array to the file
    fclose(file);  // Close the file
    return 0;
}
  
Explanation:
The array numbers is written to the file "numbers.dat" in binary format using fwrite().

3. 📖 fread() — Reading from a File

The fread() function reads binary data from a file and stores it into a buffer. Here's the syntax:

size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
  

✨ Example: Using fread()

#include 

int main() {
    FILE *file = fopen("numbers.dat", "rb");  // Open file for reading binary data
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    int numbers[5];
    fread(numbers, sizeof(int), 5, file);  // Read data from the file
    fclose(file);  // Close the file

    for(int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);  // Print the numbers
    }

    return 0;
}
  
Explanation:
This code reads 5 integers from the file "numbers.dat" and prints them to the screen using fread().

4. ❌ fclose() — Closing a File

After all file operations are completed, it's crucial to close the file using fclose(). This releases the resources used by the file.

int fclose(FILE *stream);
  

✨ Example: Using fclose()

#include 

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    fprintf(file, "This file will now be closed.");
    fclose(file);  // Close the file
    printf("File closed successfully.\n");

    return 0;
}
  
Explanation:
After writing to the file, we use fclose() to close the file and free up system resources.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review