Welcome! Log In Create A New Profile

Advanced

reading from sd

Posted by trottolo 
reading from sd
April 07, 2009 03:59PM
hello guys...
I have another request....is there any library could hrl reading from sd slot?

thx
Amedeo
Re: reading from sd
April 07, 2009 04:08PM
hrl?

:D



Edited 1 time(s). Last edit at 04/07/2009 04:08PM by daniel_c_w.
Re: reading from sd
April 07, 2009 07:44PM
hrl= help ...but I don't know why I wrote that....



Edited 1 time(s). Last edit at 04/07/2009 07:46PM by trottolo.
Re: reading from sd
April 07, 2009 08:01PM
libfat, which is installed as part of devkitpro.

#include fat.h // Should have angle brackets but this forum doesn't display them
...
fatInitDefault();
fatMountSimple("sd", &__io_wiisd);

From then on you can access files on the SD card by just using the regular C file operations. Just add sd:/ (or whatever you mounted the drive with) to the beginning of your path name.

Oh yeah, you have to link with -lfat too, and I found the order important. I have mine declared at the beginning of my library list, after -ldb.

Thanks,
cdmac
Re: reading from sd
April 08, 2009 08:23AM
thx for reply...could you add the code of your test software?
Re: reading from sd
April 08, 2009 08:36AM
If you're just wanting to read a file then...


fatInitDefault();

FILE* f;
char buf[50] = {0};

f = fopen("/hello.txt","r"); // will be root of the SD
fread(buf,50,1,f);
fclose(f);

Re: reading from sd
April 08, 2009 08:50PM
Quote
cdmac
libfat, which is installed as part of devkitpro.

#include fat.h // Should have angle brackets but this forum doesn't display them
...
fatInitDefault();
fatMountSimple("sd", &__io_wiisd);

fatMountSimple("sd", &__io_wiisd); is not required - this is done by fatInitDefault()
Re: reading from sd
April 08, 2009 08:51PM
Quote
scanff
If you're just wanting to read a file then...


fatInitDefault();

FILE* f;
char buf[50] = {0};

f = fopen("/hello.txt","r"); // will be root of the SD
fread(buf,50,1,f);
fclose(f);


Actually, that might not be the root of the SD. Depends on if SD was mounted and set as the default devoptab device. It'd be best to do this instead:

f = fopen("sd:/hello.txt","r"); // will be root of the SD
Re: reading from sd
April 08, 2009 08:59PM
Here's a better example:


fatInitDefault(); // should only be called once

// will read in the whole file

char * buffer; // file buffer
int size = 0; // file size
FILE* file = fopen("sd:/hello.txt","rb");

if(file > 0)
{
	struct stat fileinfo;
	fstat(file->_file, &fileinfo);
	size = fileinfo.st_size;	
	buffer = (char *)malloc(size);
	fread(buffer, 1, size, file);
	fclose(file);
}

// call this when you're done with the file data
free(buffer);

Re: reading from sd
April 11, 2009 01:04PM
Quote
Tantric
Here's a better example:
This was what I was looking for...perfect example!!!



fatInitDefault(); // should only be called once

// will read in the whole file

char * buffer; // file buffer
int size = 0; // file size
FILE* file = fopen("sd:/hello.txt","rb");

if(file > 0)
{
	struct stat fileinfo;
	fstat(file->_file, &fileinfo);
	size = fileinfo.st_size;	
	buffer = (char *)malloc(size);
	fread(buffer, 1, size, file);
	fclose(file);
}

// call this when you're done with the file data
free(buffer);

Code Dump???
April 17, 2009 03:17AM
I am not sure what is wrong with this code....
It compiles with no issues but when I run it I just get a Code Dump.
There are arrows around the include names but the forum doesn't show them.

#include stdio.h
#include fat.h
....
char buffer[20] = {0};
FILE* fp;

fatInitDefault();
fp=fopen("sd:/gamefile.dat","rb");
fread(buffer, 20, 1, fp);
printf("%s\n",buffer);
fclose(fp);

Anyone see the problem?

My makefile has the following:
LIBS := -lfat -lwiiuse -lbte -logc -lm
Does the order matter? Am i missing a library?
There are no compile or linking error messages.
Re: reading from sd
April 17, 2009 06:20AM
I can't see a problem. Looks like you have the correct libs.

Maybe your buffer[20] is not big enough. You could always put a NULL check on the file to ensure it's actually opening the file.

FILE* fp = 0;
fp=fopen("sd:/gamefile.dat","rb");
if(!fp) exit(0);

Also don't forget in the printf("%s\n",buffer); your buffer is treated as text and needs to be NULL terminated. So if you're reading 20 bytes from the file then your buffer should be buffer[21] = {0} otherwise the printf will overrun the buffer.
Re: reading from sd
April 17, 2009 11:30PM
Well sure enough, it is not opening my file so when I try to read the file I get a Code Dump.

But as to why it can't open the file, I don't know. The code looks right....
I put the file on the root of the SD card. File has the right name.
I even put a copy of the file in the same directory as my application just in case it wasn't looking in the root dir.
Still no luck.

*******************************
PROBLEM SOLVED!!!!!

1) If I use "sd:/filename" it does NOT work but if I use just plain "filename" it does work
2) SDHC does not work but a plain SD card does - this now explains why on some of the Homebrew I get a Core Dump

I updated my DEVKITPRO to the latest and recompiled and now SDHC WORKS!!!!!



Edited 2 time(s). Last edit at 04/19/2009 02:30PM by csanii.
Re: reading from sd
July 01, 2009 12:10AM
Arg, I keep getting a code dump I need help @_@

It compiles fine, and I included the lib.

It works fine until I press A, then it just barfs all over my screen, with the whole DSI exception, Stack Dump, and Code Dump :P

Here is the two functions that I use:

#include 
#include 
#include 
#include 
#include 
#include "FileIO.h"

namespace FileIO{
 
	void InitFat(){
		//__io_wiisd.startup();
		//fatMountSimple("sd", &__io_wiisd);
		fatInitDefault();
		fatMountSimple("sd", &__io_wiisd);
	}
	
//This is the function it crashes on...
	void SDTest(){
		FILE* test;
		test = fopen("sd:/test.txt", "rw");
		fprintf(test, "Hello World!\n");
		fclose(test);
	}
}

I call InitFat once then run the SDTest and it crashes there.

I get all the complicated stuff for the game done, but I can't figure out simple saving and loading #_#

P.S. I know the code is bad, but I just want this to work so I could start making the actual saving and loading system.
Re: reading from sd
July 01, 2009 12:37AM
figured it out -_-

so aparently "fatMountSimple("sd", &__io_wiisd);" just doesn't work o.O

anyhow, here is the working code:

#include 
#include 
#include 
#include 
#include 
#include "FileIO.h"

namespace FileIO{
 
	void InitFat(){
		//__io_wiisd.startup();
		//fatMountSimple("sd", &__io_wiisd);
		fatInitDefault();
		//fatMountSimple("sd", &__io_wiisd);
	}
	
	void SDTest(){
		FILE* test;
		test = fopen("test.txt", "w");
		fprintf(test, "Hello World!\n");
		fclose(test);
	}
}

Re: reading from sd
July 02, 2009 08:46AM
I use:
#include sdcard/wiisd_io.h

And:
__io_wiisd.startup();
fatMountSimple("sd", &__io_wiisd);
to mount the sd card

and:
fatUnmount("sd");
__io_wiisd.shutdown();
to unmount it again.

If you do IOS Reloads or want to change the sd card inside the program you need the unmount. Also i prefer fatmountsimple because it only touches the sd card, while fatinitdefault inits ALL storage devices it finds, if you have an usb pen connected at the same time, it gets initialised too.

I never got any code dumps because of this code, and also you can accesss all files with "sd:/filename" without problems, or change "sd" to "fat" for example if you use both sd and usb, just not at the same time, and want to use the same filenames for both.
Re: reading from sd
July 02, 2009 09:59AM
fatMountSimple, fatMount and fatUnmount automatically handle device startup() and shutdown() functions, there shouldn't be any need for you to call them twice.

Another interest of fatInitDefault is to set the first detected FAT device as default IO device so that you can simply access files without specifying "sd:" or "usb:". By the way, I don't think "fat:" is supported anymore with the current interfaces.
Re: reading from sd
July 02, 2009 12:25PM
Was that always there? Thanks for the hint.

You can still use "fat" if you want. I use that for example in my Gecko OS mod that loads Ocarina from sd and usb. Only if you use fatinitdefault, you can't use "fat" anymore, and i already told why i don't recommend using fatinitdefault.
Sorry, only registered users may post in this forum.

Click here to login