Writte to file?(Devkitpro)

Discussion in 'NDS - Emulation and Homebrew' started by DragonRider, Mar 12, 2012.

  1. DragonRider
    OP

    DragonRider GBAtemp Regular

    Member
    102
    1
    Aug 25, 2011
    Australia
    Hello,
    can anyone tell me how i can writte something to a file using devkitpro (and c)?
    Thanks
     
  2. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,537
    21,496
    Sep 13, 2009
    Poland
    Gaming Grotto
    1. Initialize fatlib and create a FILE variable.
    2. Open a file
    3. fwrite your buffer

    Code:
    #include 
    
    fatInitDefault();
    
    FILE* aFile;
    char* buffer;
    
    chdir("fat:/YourPath");
    aFile = fopen ("aFileYouWantToCreate.Extension" , "wb" );
    fwrite (buffer , 1 , sizeof(buffer) , aFile );
    fclose (aFile);
    EDIT: Fixed
     
  3. DragonRider
    OP

    DragonRider GBAtemp Regular

    Member
    102
    1
    Aug 25, 2011
    Australia
    thank you :)
     
  4. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,537
    21,496
    Sep 13, 2009
    Poland
    Gaming Grotto
    It's a very simplified version - remember to malloc memory to the buffer. :)

    Code:
    buffer = malloc(sizeof(YourContent));
     
  5. DragonRider
    OP

    DragonRider GBAtemp Regular

    Member
    102
    1
    Aug 25, 2011
    Australia
    Thank you, but i get this error:
    ...../main.c:41: undefined reference to `fatDefau
    ltinit'

    How can i fix it?
     
  6. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,537
    21,496
    Sep 13, 2009
    Poland
    Gaming Grotto
    You are correct - I made a boo boo. It was supposed to be fatInitDefault(); not the other way around, correct that and it'll work. Sorry, routine kills sometimes. ;)

    Also, remember to change the makefile and #include the library if you still haven't:

    Code:
    Change line:
    
    LIBS:= -lnds9
    
    Into:
    
    LIBS:= -lfat -lnds9
    A useful read:

    http://chishm.drunke...ers.com/libfat/
     
  7. DragonRider
    OP

    DragonRider GBAtemp Regular

    Member
    102
    1
    Aug 25, 2011
    Australia
    hm, i still get the error :(
    Fixxed!

    That was my problem:


    You was faster xD


    Now it work!
    Thank you
     
  8. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,537
    21,496
    Sep 13, 2009
    Poland
    Gaming Grotto
    You sure you changed the line, #include'ed and written the correct code? By the way, remember that FAT doesn't work on emulators.

    EDIT: Great, I'm glad I could help. :) Sorry, sometimes I completely forget about the makefile, I have one pre-prepared one and I just copy-paste it to my projects so I completely forget about that part. ;)