Page 1 of 1

Using fopen() in cc65

Posted: Tue Oct 03, 2017 12:41 pm
by 8bit-Dude
Hey guyz,

I am coding a game for Atari 8bit, and want to use fopen() in cc65 to read some game related data.

I used mkatr.exe to create a bootable ATR, containing only my prog and data file.
However, I cannot seem open the file (I tried both upper and lower case filename).
fopen("myfile.dat") always return 0.

Does anyone know if a specific ATR format (SpartaDOS...) is required for fopen() to work?

Cheers,
Tony

Re: Using fopen() in cc65

Posted: Tue Oct 03, 2017 8:47 pm
by whartung
No reason it should. The Ataris CIO is the moderator between your code and the actual disk OS, and it standardized all of the conventional I/O operations. (Extended stuff would have to be done via the equivalent of the XIO call, which is mostly just a raw call to CIO).

I don't know what a "bootable ATR" is, but if you're loading your code straight from the boot segment (or, say, a ROM Cartridge), vs having DOS load it via a command or whatever the equivalent of "autorun" your DOS uses, you probably don't have DOS loaded at all. That would explain the failure as well.

Re: Using fopen() in cc65

Posted: Tue Oct 03, 2017 10:21 pm
by barrym95838
8bit-Dude wrote:
fopen("myfile.dat") always return 0.
Doesn't fopen() typically require two arguments?

FILE * fopen ( const char * filename, const char * mode );

Mike B.

Re: Using fopen() in cc65

Posted: Wed Oct 04, 2017 10:58 am
by 8bit-Dude
Thanks for the replies guys!

Here is a sample of my code:

Code: Select all

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <unistd.h>

void LoadBitmap(char *filename) 
{
	FILE* fp;
	
	// Open Map File
	fp = fopen(filename, "rb");	
    
    if (fp>0) {
        printf("Loading...\n");
        fread((char*)(0xA100), 1, 0x1E00, fp);
        fclose(fp);
    } else {
        printf("File not found...\n");
    }
}

int main (void)
{
    // Try loading file
    LoadBitmap("freeway.dat");
    printf("Ready to start MGM\n");
    cgetc (); 
        
    // Done
    return EXIT_SUCCESS;
}
When loading the resulting ATR file (with compiled prog and data file), I get the message file not found.
I have attached the ATR file (loadable in Altirra) and Sources.

Re: Using fopen() in cc65

Posted: Wed Oct 04, 2017 11:43 am
by rwiker
Note that fp is a pointer; the check should be "fp != NULL" (or possibly "fp != 0"), not "fp > 0"...

... assuming Unix-like (or POSIX-like) semantics of fopen.

Re: Using fopen() in cc65

Posted: Wed Oct 04, 2017 11:52 am
by EdwardianDuck
As alluded to above, you need some form of DOS on the disk as well (or at least the A8 needs to have a DOS loaded). While the ATR is in SpartaDOS/X format, it doesn't actually contain SDX itself. Of course, you might be booting SDX from cartridge.

Attached (hopefully) is an ATR with Atari DOS 2.5 on it, with your program (renamed to AUTORUN.SYS) and data file on it. When booting this in Altirra, I no longer get the error message.

I've left the whole Atari DOS 2.5 on the image, but some of this can be deleted.

Jeremy

Re: Using fopen() in cc65

Posted: Wed Oct 04, 2017 11:56 am
by 8bit-Dude
Ed, that's a fantastic reply, thank you so much!

Could you kindly tell me how I can compile such disk myself?
Did you use mkatr, or some other program?

Re: Using fopen() in cc65

Posted: Wed Oct 04, 2017 12:13 pm
by EdwardianDuck
I'm at work without access to hobby stuff, so I cheated a bit by taking an existing ATR from a program I developed, deleted my program using Tools > Disk Explorer in Altirra then dragged and dropped your files into the image. I then renamed FREEWAY.COM to AUTORUN.SYS (again in Disk Explorer).

I can't remember exactly where I got the Atari DOS 2.5 ATR from, but it was easy enough to find online at the time I was writing the program.

Disk images are available here for various DOSes.

https://atariwiki.org/wiki/Wiki.jsp?pag ... %20DOS%202

Hopefully this helps.

Jeremy

Re: Using fopen() in cc65

Posted: Wed Oct 04, 2017 12:50 pm
by 8bit-Dude
SUP, I got the same idea working with "atadim.exe"!

If you may have a little extra time, could you please look into my next problem here: viewtopic.php?f=2&t=4933

Thank you in advance!!

Re: Using fopen() in cc65

Posted: Wed Oct 04, 2017 2:40 pm
by barrym95838
Wow, Jeremy, you jumped right in out of nowhere and provided some excellent help! Thank you. You should Introduce yourself and tell us a bit about your background.

Mike B.