1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdio>
#include <cstdlib>
int main() {
FILE* file = fopen("example.txt", "w");
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
fprintf(file, "Hello, World!\n");
fclose(file);
file = fopen("example.txt", "r");
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
char buffer[100];
if (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("Read from file: %s", buffer);
}
fclose(file);
return EXIT_SUCCESS;
}
Back to cstdio