File Handling

Section 5: File Handling

Lesson 1: File Operations in C

1.1 Reading and Writing to Files in C

File operations in C involve handling files for reading and writing data. The standard library provides functions like fopen, fread, fwrite, and fclose for file operations.

Example (Reading and Writing to Files in C):

#include <stdio.h>


int main() {

    // File pointer

    FILE* file;


    // Writing to a file

    file = fopen("example.txt", "w");

    if (file != NULL) {

        fprintf(file, "Hello, File!\n");

        fclose(file);

    }


    // Reading from a file

    char buffer[50];

    file = fopen("example.txt", "r");

    if (file != NULL) {

        fgets(buffer, sizeof(buffer), file);

        printf("File Content: %s", buffer);

        fclose(file);

    }


    return 0;

}


1.2 Error Handling for File Operations

Proper error handling is crucial for file operations to ensure the program responds appropriately to file-related issues.

Example (Error Handling for File Operations):

#include <stdio.h>


int main() {

    // File pointer

    FILE* file;


    // Writing to a file with error handling

    file = fopen("example.txt", "w");

    if (file != NULL) {

        fprintf(file, "Hello, File!\n");

        fclose(file);

    } else {

        perror("Error opening file for writing");

    }


    // Reading from a file with error handling

    char buffer[50];

    file = fopen("example.txt", "r");

    if (file != NULL) {

        if (fgets(buffer, sizeof(buffer), file) != NULL) {

            printf("File Content: %s", buffer);

        } else {

            perror("Error reading from file");

        }

        fclose(file);

    } else {

        perror("Error opening file for reading");

    }


    return 0;

}


Lesson 2: File Handling in C++

2.1 Introduction to File Streams

C++ provides file streams (ifstream for input and ofstream for output) that simplify file operations. These streams are part of the <fstream> header.

Example (Introduction to File Streams in C++): 

#include <iostream>

#include <fstream>


int main() {

    // Writing to a file using ofstream

    std::ofstream outFile("example.txt");

    if (outFile.is_open()) {

        outFile << "Hello, File!\n";

        outFile.close();

    } else {

        std::cerr << "Error opening file for writing." << std::endl;

    }


    // Reading from a file using ifstream

    std::ifstream inFile("example.txt");

    std::string content;

    if (inFile.is_open()) {

        std::getline(inFile, content);

        std::cout << "File Content: " << content << std::endl;

        inFile.close();

    } else {

        std::cerr << "Error opening file for reading." << std::endl;

    }


    return 0;

}


2.2 Reading and Writing Files in C++

File streams in C++ offer convenient syntax for reading and writing files.

Example (Reading and Writing Files in C++):

#include <iostream>

#include <fstream>


int main() {

    // Writing to a file using ofstream

    std::ofstream outFile("example.txt");

    if (outFile.is_open()) {

        outFile << "Hello, File!\n";

        outFile.close();

    } else {

        std::cerr << "Error opening file for writing." << std::endl;

    }


    // Reading from a file using ifstream

    std::ifstream inFile("example.txt");

    std::string content;

    if (inFile.is_open()) {

        std::getline(inFile, content);

        std::cout << "File Content: " << content << std::endl;

        inFile.close();

    } else {

        std::cerr << "Error opening file for reading." << std::endl;

    }


    return 0;

}

Understanding file handling in both C and C++ is essential for creating programs that interact with external data sources. Practice reading and writing files, and ensure proper error handling for robust file operations.