Program Using fopen() and fclose()
Subject: PPS (Programming for Problem Solving)
Contributed By: Sanjay
Created At: February 3, 2025
Question:
Write a C Program Using fopen() and fclose()
Explanation Video:
Explanation:
Source Code:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("example.txt", "w"); // Open file in write mode
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(fp, "Hello, this is a test file!\n"); // Write to file
fclose(fp); // Close the file
printf("File written successfully!\n");
return 0;
}
Output:
File written successfully!
(A file named example.txt is created with the text inside.)