Here is an example of using (mostly) the C library functions to write and read data to/from a file. The _filetype
is an addition to select the file type. For data storage on the CBM machines, sequential files are common, as in the example:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main (void) {
FILE *fp;
char data_out[] = "This is my data to save.";
char c;
//Saving
_filetype = 's';
if ((fp = fopen ("filename", "w")) == 0) {
printf ("File could not be opened\n\r");
exit (1);
}
fwrite (data_out, 1, sizeof (data_out), fp);
fclose (fp);
//Reading
_filetype = 's';
if ((fp = fopen ("filename", "r")) == 0) {
printf ("File could not be opened\n\r");
exit (1);
}
while (1) {
c = fgetc (fp);
if (feof (fp)) {
break;
}
printf ("%c", c);
}
fclose (fp);
}
Compile the program with cl65 progname.c -o progname.prg
and run it on a C64 with disk drive. It saves the string and then reads it back.
Note that this approach links a lot of code to the program, if you want a smaller solution, you should look into directly calling the Kernal functions, see CBM-specific functions in the cc65 documentation.