Reading/Writing Files

Tenry
Posts: 14
Joined: Tue Jun 01, 2010 11:02 am
Location: Germany

Reading/Writing Files

Post by Tenry » Mon Jul 25, 2011 9:07 pm

How do I read and write files (using libfat)?
I've searched all the internet, but I never can read or write a file.

I use fatInitDefault(), and I linked to -lfat! It always returns false.
Also, whenever using fopen() after "initializing" (with "test.txt" or "fat:/test.txt"), either reading or writing mode, it returns NULL.

What am I doing wrong? I've got the directory "filesystem" along with arm7, arm9 and include (propably from a template), and I've put the file "test.txt" in the filesystem directoy.

WinterMute
Site Admin
Posts: 1853
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Reading/Writing Files

Post by WinterMute » Mon Jul 25, 2011 10:38 pm

How are you running the code?
Help keep devkitPro toolchains free, Donate today

Personal Blog

HeavyDude
Posts: 14
Joined: Sun Oct 31, 2010 12:55 pm

Re: Reading/Writing Files

Post by HeavyDude » Mon Jul 25, 2011 11:54 pm

Unless I'm very much mistaken you have to be using a real DS rather than an emulator.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Reading/Writing Files

Post by elhobbs » Tue Jul 26, 2011 2:22 am

unless you are using a card with argv support then the current directory is the root directory. so, if you have a file in a directory named "filesystem" then you will need to use the full path to the file - /filesystem/test.txt

for a simple test put test.txt in the root directory and see if you can open it there using just the file name without the full path.

WinterMute
Site Admin
Posts: 1853
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Reading/Writing Files

Post by WinterMute » Tue Jul 26, 2011 10:54 am

HeavyDude wrote:Unless I'm very much mistaken you have to be using a real DS rather than an emulator.
desmume can generate a disk image using --cflash-path or use a ready made image. Applications using nitrofs will also work with desmume as is.

I don't think argv is relevant here (yet) since he's specified "fat:/test.txt" - the main possibilities are running under emulation or using some card which doesn't support DLDI.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Tenry
Posts: 14
Joined: Tue Jun 01, 2010 11:02 am
Location: Germany

Re: Reading/Writing Files

Post by Tenry » Tue Jul 26, 2011 11:49 am

Ah, I see, no support by emulators (like no$gba and DeSmuMe)?

Okay, I've tested some code on my DS:

Code: Select all

if(!fatInitDefault()) printf("Init FAT: Error!\n");
  else
  {
    FILE *fp = fopen("fat:/test.bin", "rb");
    if(!fp) printf("Reading fat:/test.bin: Error!\n");
    else
    {
      fclose(fp);
    }
    fp = fopen("fat:/Tetris.sav", "wb");
    if(!fp) printf("Writing fat:/Tetris.sav: Error!\n");
    else
    {
      printf("Write something to fat:/Tetris.sav\n");
      fputc(0x78, fp);
      fclose(fp);
    }
    
    fp = fopen("fat:/Tetris.sav", "rb");
    if(!fp) printf("Reading fat:/Tetris.sav: Error!\n");
    else
    {
      u8 c = fgetc(fp);
      fclose(fp);
      printf("Char read: 0x%02X\n", c);
    }
  }
It tries the following:
  • Initialize Fat (successes)
  • Open test.bin (which I'd like to have in the nds-rom; fails)
  • Write, then read Tetris.sav (successes)
I found out the file is located in the top directory of my card, instead of the sub "games" directory, but now I know I have to care about it.

There's still a final problem: (How) can I add files to the nds rom filesystem? I found out with ndstool and a commercial rom that it contains lots of files, and the "fatSize" is != 0. Or is there no support for something like this for ndstool or libnds?
As far as I know I could avoid the 4 MB limit, I seem to have, using "files" in the rom.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Reading/Writing Files

Post by elhobbs » Tue Jul 26, 2011 4:19 pm

WinterMute wrote:I don't think argv is relevant here (yet) since he's specified "fat:/test.txt" - the main possibilities are running under emulation or using some card which doesn't support DLDI.
it is relevent since it effects the current directory.

WinterMute
Site Admin
Posts: 1853
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Reading/Writing Files

Post by WinterMute » Tue Jul 26, 2011 11:26 pm

have a look at the nitrodir example under filesystem/nitrofs - it adds files to nitrofs.

It's also probably best if you don't use a custom arm7 binary - you're likely to have a lot of issues keeping your code up to date.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Tenry
Posts: 14
Joined: Tue Jun 01, 2010 11:02 am
Location: Germany

Re: Reading/Writing Files

Post by Tenry » Wed Jul 27, 2011 1:17 am

WinterMute wrote:have a look at the nitrodir example under filesystem/nitrofs - it adds files to nitrofs.
Ah okay, that might solve my issue.
Can I initialize fat AND nitrofs both in my project, so that I'm able to read nitrofs files and read/write fat files at any time?
Would be fopen("test.txt", "rt") then read the file from the nds rom?
WinterMute wrote:It's also probably best if you don't use a custom arm7 binary - you're likely to have a lot of issues keeping your code up to date.
Who says I'm using a custom arm7 binary? Or what do you mean?

WinterMute
Site Admin
Posts: 1853
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Reading/Writing Files

Post by WinterMute » Wed Jul 27, 2011 10:43 am

Sorry, I guess I misunderstood this bit
Shy Guy wrote: What am I doing wrong? I've got the directory "filesystem" along with arm7, arm9 and include (propably from a template), and I've put the file "test.txt" in the filesystem directoy.
Initialising nitrofs will initialise FAT anyway when booting from an argv supporting slot1 card in which case argv[0] holds the full path of your nds file. I'm still not sure what best practice might be when dealing with this situation though. You could potentially parse the nds path from argv[0] and read write files from that directory but, if you do, then you should preserve the device name and not attempt to use explicit device names. In DSi mode from sudukohax the device name will be sd: rather than fat: and I have some other device names in mind for other situations.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests