]> git.sur5r.net Git - cc65/blob - testcode/lib/fileio-test.c
PET screen memory is at $8000, not $0800
[cc65] / testcode / lib / fileio-test.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <fcntl.h>
5
6
7
8 FILE* Fopen (const char* Name, const char* Mode)
9 {
10     FILE* F;
11     printf ("Opening %s(%s): ", Name, Mode);
12     F = fopen (Name, Mode);
13     if (F) {
14         printf ("Ok (%d)\n", fileno (F));
15     } else {
16         printf (strerror (errno));
17     }
18     return F;
19 }
20
21
22
23 void Fwrite (FILE* F, const void* Buf, unsigned Size)
24 {
25     size_t Res;
26     Res = fwrite (Buf, 1, Size, F);
27     printf ("Writing %u bytes to %d: %u\n", Size, fileno (F), Res);
28 }
29
30
31
32 int Fread (FILE* F, void* Buf, unsigned Size)
33 {
34     size_t Res;
35     Res = fread (Buf, 1, Size, F);
36     printf ("Reading %u bytes from %d: %u\n", Size, fileno (F), Res);
37     return Res > 0? Res : 0;
38 }
39
40
41
42 void Fclose (FILE* F)
43 {
44     printf ("Closing %d:", fileno (F));
45     if (fclose (F) == 0) {
46         printf ("Ok\n");
47     } else {
48         printf (strerror (errno));
49     }
50 }
51
52
53
54 int main (void)
55 {
56     FILE* F1;
57     FILE* F2;
58     int Res;
59     static const char text1[] = "This goes into file #1\n";
60     static const char text2[] = "This goes into file #2\n";
61     static const char text3[] = "This goes into file #3\n";
62     static const char text4[] = "This goes into file #4\n";
63     static char Buf[200];
64
65
66     F1 = Fopen ("foobar1", "w");
67     F2 = Fopen ("foobar2", "w");
68
69     Fwrite (F1, text1, sizeof (text1) - 1);
70     Fwrite (F2, text2, sizeof (text2) - 1);
71     Fwrite (F1, text1, sizeof (text1) - 1);
72     Fwrite (F2, text2, sizeof (text2) - 1);
73
74     Fclose (F1);
75     Fclose (F2);
76
77     F1 = Fopen ("foobar3", "w");
78     F2 = Fopen ("foobar4", "w");
79
80     Fwrite (F1, text3, sizeof (text3) - 1);
81     Fwrite (F2, text4, sizeof (text4) - 1);
82     Fwrite (F1, text3, sizeof (text3) - 1);
83     Fwrite (F2, text4, sizeof (text4) - 1);
84
85     Fclose (F1);
86     Fclose (F2);
87
88     F1 = Fopen ("foobar1", "r");
89     Res = Fread (F1, Buf, sizeof (Buf));
90     printf ("%.*s", Res, Buf);
91     Res = Fread (F1, Buf, sizeof (Buf));
92     Fclose (F1);
93
94     return 0;
95 }
96
97