File Handling with Standard I/O in C
In addition to low-level file I/O operations, C provides high-level file handling functions based on standard I/O. These functions allow you to perform file read/write operations using the familiar printf and scanf format specifiers.
#include <stdio.h>
int main() {
FILE* file = fopen("data.txt", "w");
if (file == NULL) {
printf("File could not be opened.\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fputs("This is a line of text.", file);
fclose(file);
file = fopen("data.txt", "r");
if (file == NULL) {
printf("File could not be opened.\n");
return 1;
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
No comments:
Post a Comment