File Handling

 File Handling


File handling in C allows you to read from and write to files on the disk. You can perform operations like opening files, reading data, writing data, and closing files.



#include <stdio.h>


int main() {

    FILE* file = fopen("data.txt", "w"); // Open the file in write mode


    if (file == NULL) {

        printf("File could not be opened.\n");

        return 1;

    }


    fprintf(file, "Hello, World!\n"); // Write data to the file

    fclose(file); // Close the file


    file = fopen("data.txt", "r"); // Open the file in read mode


    if (file == NULL) {

        printf("File could not be opened.\n");

        return 1;

    }


    char buffer[100];

    fgets(buffer, sizeof(buffer), file); // Read data from the file

    printf("Data: %s", buffer);


    fclose(file); // Close the file


    return 0;

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...