File Handling in C

C file handling uses a FILE * stream from <stdio.h>. A program can open a file, read or write data, check errors, reposition the stream and then close it.

File-Handling Workflow Top ↑

FILE *file = fopen("data.txt", "r");

if (file == NULL) {
    /* handle the error */
}

/* read or write */

fclose(file);

fopen() Modes Top ↑

ModePurposeImportant behavior
rReadFile must already exist.
wWriteCreates the file or truncates an existing file.
aAppendCreates the file if needed and writes at the end.
r+Read and writeFile must already exist.
w+Read and writeCreates or truncates the file.
a+Read and appendCreates the file if needed; writes are appended.

File-Handling Tutorials in This Section Top ↑

This hub links every intentional file-handling child page in the current C section so those pages do not depend on the XML sitemap alone.

Read a Text File Character by Character Top ↑

#include <stdio.h>

int main(void) {
    FILE *file = fopen("data.txt", "r");

    if (file == NULL) {
        perror("data.txt");
        return 1;
    }

    int ch;

    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }

    if (ferror(file)) {
        printf("An error occurred while reading the file.\n");
    }

    fclose(file);
    return 0;
}

Key correction: use an int for the return value of fgetc() so EOF can be distinguished from valid byte values.

Read a File Line by Line Top ↑

#include <stdio.h>

int main(void) {
    FILE *file = fopen("data.txt", "r");
    char line[128];

    if (file == NULL) {
        perror("data.txt");
        return 1;
    }

    while (fgets(line, sizeof line, file) != NULL) {
        fputs(line, stdout);
    }

    fclose(file);
    return 0;
}

Write Text to a File Top ↑

#include <stdio.h>

int main(void) {
    FILE *file = fopen("data.txt", "w");

    if (file == NULL) {
        perror("data.txt");
        return 1;
    }

    fprintf(file, "Welcome to plus2net\n");
    fprintf(file, "Second line\n");

    fclose(file);
    return 0;
}

Append to an Existing File Top ↑

#include <stdio.h>

int main(void) {
    FILE *file = fopen("data.txt", "a");

    if (file == NULL) {
        perror("data.txt");
        return 1;
    }

    fputs("Appended line\n", file);

    fclose(file);
    return 0;
}

Write User-Entered Lines Safely Top ↑

The old C section used gets() for this task. gets() was removed from the C standard because it cannot limit input length. Use fgets() instead.

#include <stdio.h>
#include <string.h>

int main(void) {
    FILE *file = fopen("notes.txt", "w");
    char line[100];

    if (file == NULL) {
        perror("notes.txt");
        return 1;
    }

    printf("Enter lines. Press Enter on an empty line to stop.\n");

    while (fgets(line, sizeof line, stdin) != NULL) {
        if (strcmp(line, "\n") == 0) {
            break;
        }

        fputs(line, file);
    }

    fclose(file);
    return 0;
}

File Position with fseek() and ftell() Top ↑

#include <stdio.h>

int main(void) {
    FILE *file = fopen("data.txt", "r");

    if (file == NULL) {
        perror("data.txt");
        return 1;
    }

    if (fseek(file, 10L, SEEK_SET) != 0) {
        printf("Could not reposition the file.\n");
        fclose(file);
        return 1;
    }

    long position = ftell(file);

    if (position >= 0) {
        printf("Current position: %ld\n", position);
    }

    fclose(file);
    return 0;
}

Write Binary Data Top ↑

#include <stdio.h>

int main(void) {
    int values[] = {1, 2, 3, 4, 5};
    FILE *file = fopen("binary.dat", "wb");

    if (file == NULL) {
        perror("binary.dat");
        return 1;
    }

    size_t written = fwrite(
        values,
        sizeof values[0],
        sizeof values / sizeof values[0],
        file
    );

    printf("Items written: %zu\n", written);

    fclose(file);
    return 0;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer