Homebrew Save hiscore

Cid2mizard

Well-Known Member
OP
Member
Joined
Aug 16, 2007
Messages
401
Trophies
1
Age
43
Location
Maubeuge
XP
2,436
Country
France
Hello,

I wish I save the scores in a file on the SD card , as simply as possible ... the save file is created but it does 1ko and contains nothing ...

Code:
//A sauvergarder
bool niveau_unlocked[MAX_LVL];
bool niveau_solved[MAX_LVL];
u8 niveau_best[MAX_LVL];
//End save
 
void sauvegarder()
{
    fsInit();
 
    u32 bytesWrite;
    Handle fileHandle;
 
    char filename[15] = "/TILES_2DS.SAV";
 
    //setup SDMC archive
    FS_archive sdmcArchive = (FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
 
    //create file path struct
    FS_path filePath = FS_makePath(PATH_CHAR, filename);
 
    //create file
    FSUSER_OpenFileDirectly(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_READ|FS_OPEN_WRITE|FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
 
    //Write file
    for (i = 0; i < MAX_LVL; i++)
    {
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_unlocked[i], sizeof(niveau_unlocked[i]), FS_WRITE_FLUSH);
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_solved[i], sizeof(niveau_solved[i]), FS_WRITE_FLUSH);
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_best[i], sizeof(niveau_best[i]), FS_WRITE_FLUSH);
    }
 
    //Close file
    FSFILE_Close(fileHandle);
    svcCloseHandle(fileHandle);
}
 

Technicmaster0

Well-Known Member
Member
Joined
Oct 22, 2011
Messages
4,406
Trophies
2
Website
www.flashkarten.tk
XP
3,497
Country
Gambia, The
Hello,

I wish I save the scores in a file on the SD card , as simply as possible ... the save file is created but it does 1ko and contains nothing ...

Code:
//A sauvergarder
bool niveau_unlocked[MAX_LVL];
bool niveau_solved[MAX_LVL];
u8 niveau_best[MAX_LVL];
//End save
 
void sauvegarder()
{
    fsInit();
 
    u32 bytesWrite;
    Handle fileHandle;
 
    char filename[15] = "/TILES_2DS.SAV";
 
    //setup SDMC archive
    FS_archive sdmcArchive = (FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
 
    //create file path struct
    FS_path filePath = FS_makePath(PATH_CHAR, filename);
 
    //create file
    FSUSER_OpenFileDirectly(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_READ|FS_OPEN_WRITE|FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
 
    //Write file
    for (i = 0; i < MAX_LVL; i++)
    {
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_unlocked[i], sizeof(niveau_unlocked[i]), FS_WRITE_FLUSH);
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_solved[i], sizeof(niveau_solved[i]), FS_WRITE_FLUSH);
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_best[i], sizeof(niveau_best[i]), FS_WRITE_FLUSH);
    }
 
    //Close file
    FSFILE_Close(fileHandle);
    svcCloseHandle(fileHandle);
}
You can look at the source of my Guess the Number. The only well-written part are the "files.h" and "files.c" files which contain exactly the methods you need. I think that you can write simply serveral saves by using one archive that contains serveral text files containing the scores.
 
  • Like
Reactions: Cid2mizard

Cid2mizard

Well-Known Member
OP
Member
Joined
Aug 16, 2007
Messages
401
Trophies
1
Age
43
Location
Maubeuge
XP
2,436
Country
France
Ok thank you both , it works now ...

Code:
void sauvegarder()
{
    char filename[15] = "/TILES_2DS.sav";
 
    FILE *file = fopen(filename,"w+b");
 
    //Write file
    for (i = 0; i < MAX_LVL; i++)
    {
        fwrite(&niveau_unlocked[i], sizeof(niveau_unlocked[i]), 1, file);
        fwrite(&niveau_solved[i], sizeof(niveau_solved[i]), 1, file);
        fwrite(&niveau_best[i], sizeof(niveau_best[i]), 1, file);
    }
 
    //Close file
    fclose(file);
}
 
 
void charger()
{
    char filename[15] = "/TILES_2DS.sav";
    u8 erreur = 0;
 
    FILE* file = fopen(filename, "r+b");
 
    if (file != 0)
    {
        for (i = 0; i < MAX_LVL; i++)
        {
            fread(&niveau_unlocked[i], 1, sizeof(niveau_unlocked[i]), file);
            fread(&niveau_solved[i], 1, sizeof(niveau_solved[i]), file);
            fread(&niveau_best[i], 1, sizeof(niveau_best[i]), file);
        }
        fclose(file);
    }
    else
    {
        erreur = 1;
    }
 
    if (erreur == 1)
    {
        sauvegarder();
    }
}
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    K3Nv2 @ K3Nv2: Pass