#include <stdio.h>
int main() {
FILE *file;
char data[100];
// Open a file in write mode
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file for writing.\n");
return 1;
}
// Write data to the file
printf("Enter data to write to the file: ");
fgets(data, sizeof(data), stdin);
fprintf(file, "%s", data);
// Close the file
fclose(file);
// Open the file in read mode
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
// Read and display the data from the file
printf("Reading data from the file:\n");
fgets(data, sizeof(data), file);
printf("Data: %s", data);
// Close the file
fclose(file);
return 0;
}