-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_3.c
53 lines (39 loc) · 937 Bytes
/
io_3.c
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct point
{
int x,y
} Point;
int main(int argc, char* argv[]) {
Point p1 = {
.x = 12, .y=20
};
Point p2 = {};
FILE* in;
FILE* out;
char buffer_in[256], buffer_out[256];
out = fopen("point.dat", "w");
if (out == NULL) {
return 1;
}
snprintf(buffer_out, 256, "%d %d\n", p1.x, p1.y);
size_t bytesWrote = fwrite(buffer_out, sizeof(char), strlen(buffer_out), out);
fclose(out);
if (bytesWrote != strlen(buffer_out)) {
return 1;
}
// READING
in = fopen("point.dat", "r");
if (in == NULL) {
return 1;
}
if (fgets(buffer_in, 256, in) == NULL) {
fclose(in);
return 1;
}
fclose(in);
sscanf(buffer_in, "%d %d\n", &p2.x, &p2.y);
printf("Read from file the point: %d %d\n", p2.x, p2.y);
return 0;
}