07-25-2023, 01:23 PM
C provides functions for reading from and writing to files.
// Writing to a file
FILE *file = fopen("data.txt", "w");
if (file != NULL) {
fprintf(file, "Hello, World!");
fclose(file);
}
// Reading from a file
char buffer[100];
file = fopen("data.txt", "r");
if (file != NULL) {
fgets(buffer, 100, file);
printf("%s\n", buffer); // Output: Hello, World!
fclose(file);
}
// Writing to a file
FILE *file = fopen("data.txt", "w");
if (file != NULL) {
fprintf(file, "Hello, World!");
fclose(file);
}
// Reading from a file
char buffer[100];
file = fopen("data.txt", "r");
if (file != NULL) {
fgets(buffer, 100, file);
printf("%s\n", buffer); // Output: Hello, World!
fclose(file);
}