Skip to content
Home » My Blog Tutorial » File Handling in C: Mastering Input/Output Operations

File Handling in C: Mastering Input/Output Operations

Files & Error Handling in C Language

File handling is a crucial aspect of C programming, allowing developers to work with files efficiently. In this blog post, we’ll explore the essential concepts of accessing, reading from, and writing to files in C. We’ll cover key functions like fopen(), fclose(), fgets(), and fprintf(), providing you with the tools to manipulate files effectively.

Accessing Files: Opening and Closing

The first step in file handling is accessing the file. C provides the FILE type and functions from the stdio.h library to manage file streams. Let’s examine how to open and close files:

#include <stdio.h>

int main() {
    FILE *fptr;

    fptr = fopen("example.txt", "w");
    if (fptr == NULL) {
        printf("Error opening file.");
        return -1;
    }
    // File operations go here
    fclose(fptr);
    return 0;
}

In this example, we use fopen() to create a new file named “example.txt” in write mode. Always check if the file opened successfully by comparing the file pointer to NULL. After performing file operations, it’s crucial to close the file using fclose() to free up system resources.

Reading from Files: Extracting Data

Once a file is open, you can read its contents using various functions. Here are some commonly used methods:

Character-by-Character Reading

The fgetc() function reads a single character from the file:

char c;
while ((c = fgetc(fptr)) != EOF) {
    printf("%c", c);
}

Line-by-Line Reading

To read entire lines, use the fgets() function:

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

Formatted Reading

For structured data, fscanf() is useful:

int num;
char str[20];
while (fscanf(fptr, "%d %s", &num, str) == 2) {
    printf("Number: %d, String: %s\n", num, str);
}

Writing to Files: Storing Data

Writing data to files is equally important. Let’s explore some writing functions:

Character-by-Character Writing

Use fputc() to write individual characters:

char c = 'A';
fputc(c, fptr);

String Writing

For writing strings, fputs() is handy:

fputs("Hello, World!\n", fptr);

Formatted Writing

fprintf() allows you to write formatted data:

int num = 42;
char str[] = "C Programming";
fprintf(fptr, "Number: %d, String: %s\n", num, str);

By mastering these file handling techniques, you’ll be able to create robust C programs that efficiently manage data storage and retrieval. Remember to always close your files and handle errors appropriately for reliable file operations.

For more information on advanced file handling techniques, check out this comprehensive guide on C file I/O.


Discover more from teguhteja.id

Subscribe to get the latest posts sent to your email.

Leave a Reply

Optimized by Optimole
WP Twitter Auto Publish Powered By : XYZScripts.com

Discover more from teguhteja.id

Subscribe now to keep reading and get access to the full archive.

Continue reading