From 1075d1e193448b3234ffd07361fa23b6039451cb Mon Sep 17 00:00:00 2001 From: cuz Date: Sun, 17 Nov 2002 23:00:14 +0000 Subject: [PATCH] New module fileio-test.c git-svn-id: svn://svn.cc65.org/cc65/trunk@1534 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- testcode/lib/fileio-test.c | 97 ++++++++++++++++++++++++++++++++++++++ testcode/lib/files.txt | 1 + 2 files changed, 98 insertions(+) create mode 100644 testcode/lib/fileio-test.c diff --git a/testcode/lib/fileio-test.c b/testcode/lib/fileio-test.c new file mode 100644 index 000000000..013d9390a --- /dev/null +++ b/testcode/lib/fileio-test.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include + + + +FILE* Fopen (const char* Name, const char* Mode) +{ + FILE* F; + printf ("Opening %s(%s): ", Name, Mode); + F = fopen (Name, Mode); + if (F) { + printf ("Ok (%d)\n", fileno (F)); + } else { + printf (strerror (errno)); + } + return F; +} + + + +void Fwrite (FILE* F, const void* Buf, unsigned Size) +{ + size_t Res; + Res = fwrite (Buf, 1, Size, F); + printf ("Writing %u bytes to %d: %u\n", Size, fileno (F), Res); +} + + + +int Fread (FILE* F, void* Buf, unsigned Size) +{ + size_t Res; + Res = fread (Buf, 1, Size, F); + printf ("Reading %u bytes from %d: %u\n", Size, fileno (F), Res); + return Res > 0? Res : 0; +} + + + +void Fclose (FILE* F) +{ + printf ("Closing %d:", fileno (F)); + if (fclose (F) == 0) { + printf ("Ok\n"); + } else { + printf (strerror (errno)); + } +} + + + +int main (void) +{ + FILE* F1; + FILE* F2; + int Res; + static const char text1[] = "This goes into file #1\n"; + static const char text2[] = "This goes into file #2\n"; + static const char text3[] = "This goes into file #3\n"; + static const char text4[] = "This goes into file #4\n"; + static char Buf[200]; + + + F1 = Fopen ("foobar1", "w"); + F2 = Fopen ("foobar2", "w"); + + Fwrite (F1, text1, sizeof (text1) - 1); + Fwrite (F2, text2, sizeof (text2) - 1); + Fwrite (F1, text1, sizeof (text1) - 1); + Fwrite (F2, text2, sizeof (text2) - 1); + + Fclose (F1); + Fclose (F2); + + F1 = Fopen ("foobar3", "w"); + F2 = Fopen ("foobar4", "w"); + + Fwrite (F1, text3, sizeof (text3) - 1); + Fwrite (F2, text4, sizeof (text4) - 1); + Fwrite (F1, text3, sizeof (text3) - 1); + Fwrite (F2, text4, sizeof (text4) - 1); + + Fclose (F1); + Fclose (F2); + + F1 = Fopen ("foobar1", "r"); + Res = Fread (F1, Buf, sizeof (Buf)); + printf ("%.*s", Res, Buf); + Res = Fread (F1, Buf, sizeof (Buf)); + Fclose (F1); + + return 0; +} + + diff --git a/testcode/lib/files.txt b/testcode/lib/files.txt index 5ed341334..0751b1495 100644 --- a/testcode/lib/files.txt +++ b/testcode/lib/files.txt @@ -7,6 +7,7 @@ cprintf.c - test program for cprintf \n and \r operators cursor.c - test the cursor() function deb.c - test program for the library debugger div-test.c - test division/modulus and the div() routine +fileio-test.c - test C file i/o routines (fopen/fread/fwrite/fclose) ft.c - file I/O test program (open + read functions) getsp.s - helper routine for ft.c joytest.c - readjoy function test program -- 2.39.5