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 > Help with saving/writing files

#168787 - Emmanuel - Sun May 24, 2009 8:45 am

So, I want to save a simple .txt file, but:

1. I try to use EFS, so I have a .txt file with one row per variable with "00000" to have enough space when writing. It reads properly, but it doesn't write. Not on no$gba, not on my R4 in real DS.

2. I try to chdir to FAT, but it says chdir is not defined and I already tried including all sorts of headers, searched some code, and I still can't get it to be included.

Any help/ideas/suggestions? This is my code

Code:

bool SaveFile::readFile(char * path){
   bool wasSuccessful = false;
   
   if(save != NULL){
      fclose(save);
      save = NULL;
   }
   save = fopen(path, "r");
   if(save != NULL) {
      int line = 0;
      int value = 0;
      while(fscanf(save, "%d", &value) != EOF){
         switch(line){
            case MAXXSCROLL_LINE:
               this->maxXScroll = value;
               break;
            case MAXYSCROLL_LINE:
               this->maxYScroll = value;
               break;
            case XBORDER_LINE:
               this->xBorder = value;
               break;
            case YBORDER_LINE:
               this->yBorder = value;
               break;
            case MAXZOOM_LINE:
               this->maxZoom = value;
               break;
            case MINZOOM_LINE:
               this->minZoom = value;
               break;
            case MAXZOOMCHANGE_LINE:
               this->maxZoomChange = value;
               break;
            case MINZOOMCHANGE_LINE:
               this->minZoomChange = value;
               break;
            case GOOMBA_ON_LINE:
               this->goomba = value;
               break;
         }
         line++;
      }

      fclose(save);
      save = NULL;
   }else{
      iprintf("File NULL");
   }

   return wasSuccessful;
}

bool SaveFile::saveFile(){
   bool wasSuccessful = false;

   if(save != NULL){
      fclose(save);
      save = NULL;
   }
   chdir("FAT:/");
   string path = *efs_path + EFS_SAVE_PATH;
   save = fopen(path.c_str(), "r");
   if(save != NULL){
      stringstream saveStream(stringstream::in | stringstream::out);
      
      saveStream <<
         maxXScroll << endl <<
         maxYScroll << endl <<
         xBorder << endl <<
         yBorder << endl <<
         maxZoom << endl <<
         minZoom << endl <<
         maxZoomChange << endl <<
         minZoomChange << endl <<
         goomba << endl;
      
      int size = sizeof(saveStream.str().c_str());
      fwrite(saveStream.str().c_str(), 1, size, save);
      fclose(save);
      save = NULL;
   }

   return wasSuccessful;
}


All the variables to save are ints except goomba which is a bool. saveFile() is called when you press A, Start or B to exit the options menu.

Here is the file in case it may be my cart or something:
http://www.mediafire.com/file/2mz1d3rnz1m/clash.nds
*Uploaded in mediafire, possible advertising.

#168788 - headspin - Sun May 24, 2009 12:21 pm

You opening the file for reading not writing. It should be

Code:
save = fopen(path.c_str(), "w+");


Also writing files does not work in No$ so you will need to test it on your R4.
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#168790 - Emmanuel - Sun May 24, 2009 2:38 pm

-facepalm- And, I had spent hours trying to find what was wrong.

Also, I'd like to add that that sizeof didn't work. Just for the first value, probably because it returned the size of the pointer, not the data.

Code:
fprintf(save, saveStream.str().c_str());

This was used to fix it.
Thanks

Still, I'd like to add an export option, so people can then import the save file when I release new compilations. So, how do I get that chdir working? Or how do I switch between FAT and EFS?