gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > I just want an example of file writing with FAT drivers

#78658 - Globoeil - Sat Apr 08, 2006 6:27 pm

Hi all
Since one month I try to work on VGMDS (see it in PALIB projects section), I work with PALIB
and I can't make a stable part of my program.
So I really need your help.
I just want something SIMPLE:

Can you paste me the source of a simple-simple code who just write a file with a name given by user (using keyboard).
I've written the function to store char array from keyboard:

void readfromK(u8 maxsize,char* data){
u 8i=0;
PA_InitKeyboard(1);
PA_KeyboardIn (27, 20);
while(true) {
tmpc=PA_CheckKeyboard();
if(tmpc=='\n' || Pad.Held.A) break;
if(data[0]!='\n') PA_OutputText(0,0,1,"%s! ",data);
PA_EraseLastKey();
if(tmpc!=0 && tmpc!=PA_BACKSPACE && i<maxsize){ data[i]=tmpc;
if(tmpc!=PA_SHIFT) i++;
wait(5);}
if(tmpc!=0 && tmpc==PA_BACKSPACE && i>0) data[--i]='';
}
PA_KeyboardOut();
}


Now all that I want is the program with the main function, save function (readfromK function too, paste it).
Thankfull for your help.
What is simple for you will be really helpfull for me.

#78814 - josath - Sun Apr 09, 2006 11:54 pm

I don't know what VGMDS, and you're going to have to give me a link, I'm not going to search for PALIB, find their projects section, then click on your app just to see what it is.

Here is some sample code. I didn't test it, but it should be correct. wherever I wrote // ERROR, it means something failed, you should print an error message to the user.

Code:

char *filename = "testfile.txt"; // filename to save to
char *buffer = "This is a test string."; // data to save into the file
int bufsize = strlen(buffer); // how much data to save

if(!FAT_InitFiles()) { // initialize the FAT library
   // ERROR
} else {
   FAT_FILE *fp = FAT_fopen(filename, "w"); // open the file
   if(!fp) {
        // ERROR
   } else {
        int bytesWritten = FAT_fwrite(buffer, 1, bufsize, fp); // write the data
        if(bytesWritten < bufsize) {
             // ERROR
        }
        FAT_fclose(fp); // close the file. DONT FORGET THIS OR DATA MAY BE LOST!
   }
}