File Handling with Standard I/O in C

 File Handling with Standard I/O in C


In addition to low-level file I/O operations, C provides high-level file handling functions based on standard I/O. These functions allow you to perform file read/write operations using the familiar printf and scanf format specifiers.



#include <stdio.h>


int main() {

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


    if (file == NULL) {

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

        return 1;

    }


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

    fputs("This is a line of text.", file);


    fclose(file);


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


    if (file == NULL) {

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

        return 1;

    }


    char buffer[100];

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

        printf("%s", buffer);

    }


    fclose(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...