Welcome! Log In Create A New Profile

Advanced

Creating file on SD Card

Posted by erikspyder 
Creating file on SD Card
October 16, 2009 03:20AM
Hi, I'm new in C and Wii development. I'm trying to create a file on the SD root. The program is compiling good but not working (fopen Error). What's wrong with the following code ?

Thanks in advance!

...
int main(int argc, char **argv)
{
FILE * file;
...
SDCard_Init();

file = fopen("SD:/Test.txt", "w");

if(!file)
{
printf("fopen Error!!");
fclose(file);
return false;
}

fprintf(file, "This is a test");

fclose(file);

SDCard_DeInit();

return 0;
}
Re: Creating file on SD Card
October 16, 2009 03:26AM
Are you using libfat? If not, what are you using?
Re: Creating file on SD Card
October 16, 2009 03:33AM
yes fat.h from libfat.
Re: Creating file on SD Card
October 16, 2009 03:41AM
i JUST starting learning about working with files and stuff on the SD card so i'm not that knowledgeable on external files, but i had a similar problem and what fixed it for me was this line:

fatInitDefault();
Re: Creating file on SD Card
October 16, 2009 07:17AM
Quote
mdbrim
i JUST starting learning about working with files and stuff on the SD card so i'm not that knowledgeable on external files, but i had a similar problem and what fixed it for me was this line:

fatInitDefault();
You hit the nail on the head there.
You must instalize the FAT file system.
By placing the function he told you about ( fatInitDefault(); ) anywhere in your code.
This must be done before you start using, manipulating, creating, or modifying files.

EDIT:

Also, for those that would like it, there is a few tutorials here: [wiibrew.org]



Edited 3 time(s). Last edit at 10/16/2009 07:23AM by ckinrun.
Re: Creating file on SD Card
October 16, 2009 09:33PM
Here is how I would've wrote it:
...
int main(int argc, char **argv)
{
FILE * file = 0;
...
fatInitDefault();

file = fopen("sd:/Test.txt", "wb");

if(!file)
{
printf("fopen Error!!");
fclose(file);
return false;
}

fprintf(file, "This is a test");

fclose(file);

//Put a pause here so you can view the output on your tv screen

//SDCard_DeInit(); Just let the loader take care of this

return 0;
}
Maybe this will work for you.
Re: Creating file on SD Card
October 17, 2009 01:45AM
That's good. Thanks a lot! It's very appreciated.



Edited 1 time(s). Last edit at 10/17/2009 01:45AM by erikspyder.
Re: Creating file on SD Card
October 17, 2009 01:49AM
Your welcome :)

Let me know if you need anything else.
Re: Creating file on SD Card
October 19, 2009 04:30PM
Quote
Arikado
Here is how I would've wrote it:
...
if(!file)
{
printf("fopen Error!!");
fclose(file);
return false;
}
...
Maybe this will work for you.

Trying to fclose NULL is not a good idea.
Re: Creating file on SD Card
October 19, 2009 09:10PM
Quote
tueidj
Quote
Arikado
Here is how I would've wrote it:
...
if(!file)
{
printf("fopen Error!!");
fclose(file);
return false;
}
...
Maybe this will work for you.

Trying to fclose NULL is not a good idea.
Very nice catch. That was in the original code and I completely overlooked it.
Sorry, only registered users may post in this forum.

Click here to login