File Handling in C

 File Handling in C


File handling is an essential aspect of programming. C provides functions to perform operations on files, such as reading from and writing to files. You can open files, read data from them, write data to them, and close them.


#include <stdio.h>


int main() {

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


    if (file == NULL) {

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

        return 1;

    }


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

    fclose(file);


    return 0;

}

In this example, the program opens a file named "file.txt" in write mode using fopen. It checks if the file was opened successfully, writes a string to the file using fprintf, and then closes the file using fclose.

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