Jump to content

How to access Files and Directories on the SD card with CC65


rje
 Share

Recommended Posts

The commands for file and directory access are in <cbm.h>.  Here are the example functions I used to try them out.

unsigned char readFile(const char *filename)
{
    char s[200];
    unsigned char lfn = 1;
    unsigned char dev = 8;
    unsigned char sec_addr = 0;
    unsigned char res = cbm_open(lfn, dev, sec_addr, filename);
    if (res == 0)
    {
        cbm_read(lfn, s, 200);
        cbm_close(lfn);
        printf("data: %s\r\n", s);
    }
    
    return res;
}

void readFile2(const char *filename)
{
    char buf[200];
    FILE *fp = fopen( filename, "r" );

    if (fp)
    {
        fgets(buf, 100, fp);
        fclose(fp);
        cprintf("data: %s\r\n", buf);
    }
}

unsigned char readDir(const char* path)
{
    struct cbm_dirent dir;
    unsigned char res = cbm_opendir(1, 8, path);
    printf("dir: [%s]\n", path);
    if (res == 0)
    {
        printf("listing for %s:\n", path);
        cbm_readdir(1, &dir); // disk header
        printf("file              blocks     type  access\n"
               "----              ------     ----  ------\n");
        while (cbm_readdir(1, &dir) == 0) 
        {
            switch(dir.type)
            {
                case 2:  textcolor(8); break;
                case 17: textcolor(7); break;
            }
            cprintf("%-18s %5u     %3s   %u\r\n", 
                dir.name, 
                dir.size, 
                dir.type == 2? "dir" : dir.type == 17? "prg" : "???",
                dir.access);
	            textcolor(1);
        }
        cbm_closedir(1);
    }
    return res;
}
Edited by rje
Link to comment
Share on other sites

Tom adds an additional note about changing directories:

Quote

To change directories on the CMDR-DOS device, you need to open a file with device 8 and secondary address 15. Set the filename to the DOS command to change directory:

"CD:directoryname"

After that, you can close the channel. (In fact, you should close it. If you don't, you will eventually get an error as the system runs out of free I/O channels.) 

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

Please review our Terms of Use