⬅ Previous Next ➡

File Handling in C

File Handling in C
  • File Handling is used to store data permanently in files (text or binary).
  • C provides file functions in <stdio.h> using a pointer of type FILE*.
  • Common steps: open file → read/writeclose file.

1) Basic File Handling Concepts

  • FILE* is used to refer to an opened file.
  • fopen() opens a file: FILE *fp = fopen("name", "mode");
  • fclose() closes the file: fclose(fp);
  • Always check fp == NULL to ensure file opened successfully.
#include <stdio.h>

int main() {
    FILE *fp = fopen("demo.txt", "w");

    if (fp == NULL) {
        printf("File open failed!\n");
        return 0;
    }

    fprintf(fp, "Hello File!\n");
    fclose(fp);

    printf("File written successfully.\n");
    return 0;
}

2) File Modes

  • r : read (file must exist)
  • w : write (creates new / overwrites existing)
  • a : append (writes at end, creates if not exist)
  • r+ : read + write (file must exist)
  • w+ : read + write (creates/overwrites)
  • a+ : read + append (creates if not exist)
  • Binary modes: add b (e.g., rb, wb, ab).

3) Writing to a File

  • Functions:
    • fprintf() - formatted write
    • fputs() - write string
    • fputc() - write single character
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "w");
    if (fp == NULL) return 0;

    fprintf(fp, "Name: Sourav\n");
    fputs("Course: C Programming\n", fp);
    fputc('A', fp);
    fputc('\n', fp);

    fclose(fp);
    return 0;
}

4) Reading from a File

  • Functions:
    • fscanf() - formatted read
    • fgets() - read line/string
    • fgetc() - read single character
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("File not found!\n");
        return 0;
    }

    char line[100];
    while (fgets(line, sizeof(line), fp) != NULL) {
        printf("%s", line);
    }

    fclose(fp);
    return 0;
}

5) File Positioning Functions (fseek, ftell, rewind)

  • ftell(fp): returns current file pointer position (in bytes).
  • fseek(fp, offset, origin): moves file pointer to a new position.
    • origin can be: SEEK_SET (start), SEEK_CUR (current), SEEK_END (end)
  • rewind(fp): moves file pointer back to the beginning.
#include <stdio.h>

int main() {
    FILE *fp = fopen("demo.txt", "w+");
    if (fp == NULL) return 0;

    fputs("ABCDEFGHIJ", fp);

    long pos = ftell(fp);
    printf("Position after write: %ld\n", pos);

    // Move pointer to 3rd index (0-based), means 4th character
    fseek(fp, 3, SEEK_SET);
    printf("After fseek to 3: %ld\n", ftell(fp));

    int ch = fgetc(fp); // reads from new position
    printf("Character at pos 3: %c\n", ch);

    rewind(fp);
    printf("After rewind: %ld\n", ftell(fp));

    fclose(fp);
    return 0;
}

6) Quick Notes

  • Always check fp == NULL to avoid crashes.
  • Use text modes for normal text and binary modes for images/audio/data files.
  • Use fseek and ftell for random access in files.
  • Always fclose() after operations to save data properly.
⬅ Previous Next ➡