Algorithm


  • Open File
    • fopen()
  • Close file
    • fclose()

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>

int main() {
    FILE *filePointer;
    char content[] = "Hello, File Handling!";

    // Open file in write mode
    filePointer = fopen("example.txt", "w");

    // Check if file opened successfully
    if (filePointer == NULL) {
        printf("Error opening the file.\n");
        return 1;
    }

    // Write content to the file
    fprintf(filePointer, "%s", content);

    // Close the file
    fclose(filePointer);

    printf("File written successfully.\n");

    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
File written successfully.
Advertisements

Demonstration


File writing in C Programming

Next
Appending into a File in C Programming