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 > Libfat renaming

#137624 - calcprogrammer1 - Tue Aug 14, 2007 4:01 am

How do you rename using libfat? I thought it was:

rename(char* oldname, char* newname);

but this is my code:
Code:
void renameFile(char *file){
char * oldfile = file;
PA_16cText(0,0,17,255,33,"Rename File:",1,1,100);
PA_16cText(0,0,34,255,50,file,1,1,100);
s32 nletter = strlen(file);
char letter = 0;
while(letter != '\n'){
     letter = PA_CheckKeyboard();
     if(letter > 31){
          file[nletter] = letter;
          //clear screen, update text
     }else if((letter == PA_BACKSPACE)&&nletter){
          nletter--;
          file[nletter] = ' ';
          //clear screen, update text
     }
     PA_WaitForVBL();
}
PA_ClearBg(0,2);
rename(oldfile,file);
}


Problem is, this code does nothing on Desmume (once you hit Enter it doesn't rename, but it still runs), and crashes on real DS (and when rebooted, it's not renamed).
_________________
DS Firmware 1, Datel Games n' Music card / Chism's FW hacked GBA MP v2 CF

There's no place like 127.0.0.1.

#137625 - wintermute - Tue Aug 14, 2007 4:03 am

rename doesn't work in the current toolchain.

http://forum.gbadev.org/viewtopic.php?p=133075#133075
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#137635 - DragonMinded - Tue Aug 14, 2007 6:22 am

Does anyone else notice that he's assigning oldfile to the pointer of file, then overwriting the contents of file with the new data? Basically oldfile and file are aliases and both contain the same data when it hits the rename, as well as the fact that you are overwriting an input pointer and have unspecified amount of memory.
_________________
Enter the mind of the dragon.

http://dragonminded.blogspot.com

Seriously guys, how hard is it to simply TRY something yourself?

#137638 - calcprogrammer1 - Tue Aug 14, 2007 7:31 am

So, you're saying that I shouldn't pass "file" by reference? I though passing by reference (or using a pointer) was "char &file" not "char *file". From what I found, char* is a character type with an unspecified number of positions, because I was having problems using char [256] I used char*.

So if I change it to

void renameFile(char file){
char oldfile [256] = file;

that it'd work? I'll try putting an output statement at both ends of that and see if they're the same or not.
_________________
DS Firmware 1, Datel Games n' Music card / Chism's FW hacked GBA MP v2 CF

There's no place like 127.0.0.1.

#137639 - DragonMinded - Tue Aug 14, 2007 8:03 am

You need to do some reading up on pointers and string operations in C. You can't just assign a string to another like that.
_________________
Enter the mind of the dragon.

http://dragonminded.blogspot.com

Seriously guys, how hard is it to simply TRY something yourself?

#137640 - calcprogrammer1 - Tue Aug 14, 2007 8:14 am

Oh...lol, forgot

Wonder why I did that, I'm assuming you're referring to strcpy() and such.

All the other places in my program I used strcpy() but this I just realized is...not done right. The keyboard input thing works, it was copied from PAlib tutorial (I think the whole thing was copied from PAlib tutorial actually).

So.

void renameFile(char file){
char oldfile [256];
strcpy(oldfile,file);
...
...

That should do the trick, but the question remains, does the rename() function work or will it still crash? It's 2:12AM and I'm tired, maybe that's why I wasn't thinking, though I wrote the rename function really quickly, focusing mainly on trying to enable the keyboard, and when I found out that the keyboard didn't work, I put the project on hold for like a week, now I realize I should've commented the code some more. I'll try it tomorrow.
_________________
DS Firmware 1, Datel Games n' Music card / Chism's FW hacked GBA MP v2 CF

There's no place like 127.0.0.1.

#137652 - elhobbs - Tue Aug 14, 2007 1:52 pm

make sure that the new file name you are creating is null terminated - doesn't look like it is from your partial code posting.

#137654 - psychowood - Tue Aug 14, 2007 2:05 pm

calcprogrammer1 wrote:
but the question remains, does the rename() function work or will it still crash?


Nope, as wintermute said, rename doesn't work in current toolchain.
_________________
DLDIrc Developer

#137659 - elhobbs - Tue Aug 14, 2007 3:05 pm

it may not work correctly, but I do not think that it should crash

#137660 - psychowood - Tue Aug 14, 2007 3:09 pm

elhobbs wrote:
it may not work correctly, but I do not think that it should crash


Yep, no crash, it should (and it does, at least it did when I tried it some months ago) simply do nothing.
_________________
DLDIrc Developer