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 > Getting a file size

#156502 - NeX - Sat May 10, 2008 9:16 pm

So I've finally got my hands on something DLDI. An R4, to be precise. I want the size of a file. I get no file error, just always a daft filesize. Each file in the directory, ranging from 2 to 200kb, reads as "1179923538". The file in this example is 33,210 bytes. What am I not understanding?

Code:

                     soundf = fopen (filename[sounds], "r");
                     if(stat("kick0_maypex.raw",&st)==0)
                     {
                        iprintf("Size: %d\n",st.st_size);
                     }
                     else
                     {
                        iprintf("Couldn't read the file!\n");
                     }

_________________
Strummer or Drummer?.
Or maybe you would rather play with sand? Sandscape is for you in that case.

#156504 - elhobbs - Sat May 10, 2008 9:34 pm

Code:
if(stat("kick0_maypex.raw",&st)==0)

you are always passing the same filename to stat.

#156505 - NeX - Sat May 10, 2008 9:35 pm

Yes, to make sure it wasn't looking for a nonsense filename. It is not returning a filesize. It's returning rubbish.
_________________
Strummer or Drummer?.
Or maybe you would rather play with sand? Sandscape is for you in that case.

#156513 - NeX - Sat May 10, 2008 11:16 pm

If you're wondering what errno is being returned, zero. There is no problem opening the file, and reading from it, just getting the stats.
_________________
Strummer or Drummer?.
Or maybe you would rather play with sand? Sandscape is for you in that case.

#156514 - Lazy1 - Sun May 11, 2008 12:08 am

Try:
Code:

fstat( fileno( soundf ), &st );

#156535 - zeruda - Sun May 11, 2008 12:04 pm

You could try something along the lines of the following:

Code:
void ReadFile(const char *Name)
{
    ifstream infile;
    char FileName[80];
    strcpy (FileName, Name);
    strcat (FileName, ".raw");
    infile.open (FileName, ios::ate | ios::binary);
    int size = infile.tellg();           // Since we are at the end, we can get the size
    infile.seekg(0, ios::beg);              // Seek back to the beginning
    u32 *Data = new u32[(size/4)];           // use a quarter of the size since using u32
    for (int i = 0; i < size; i++) {
        infile.read((char*)&Data[i], 4);
    }
    infile.close();
}

#156538 - tepples - Sun May 11, 2008 12:50 pm

Often, especially with the GNU libstdc++ used in devkitARM, you get smaller code by doing something in <cstdio> (or <stdio.h> in C) instead of <iostream>. In <cstdio>, you'd use fopen, fseek, ftell, then fclose or rewind.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#156554 - NeX - Sun May 11, 2008 3:32 pm

Thanks for trying to help, but it's still not working. I think it might be a problem with the MicroSD card; it's taking up to a minute to write the 90kb .nds. I'm still getting garbage for the filesizes. That alternate method looked perfect, but I can't get it to compile. Is that for C++?
_________________
Strummer or Drummer?.
Or maybe you would rather play with sand? Sandscape is for you in that case.

#156557 - simonjhall - Sun May 11, 2008 4:40 pm

I find the formatting of a flash card that goes into a DS will eventually "wear out" for some reason and will need a re-format. Maybe it's that time again?
_________________
Big thanks to everyone who donated for Quake2

#156560 - silent_code - Sun May 11, 2008 5:27 pm

simonjhall wrote:
I find the formatting of a flash card that goes into a DS will eventually "wear out" for some reason and will need a re-format. Maybe it's that time again?

be asured, this isn't only true for "a flash card that goes into a DS". :^|

#156651 - zeruda - Mon May 12, 2008 3:49 am

NeX wrote:
Thanks for trying to help, but it's still not working. I think it might be a problem with the MicroSD card; it's taking up to a minute to write the 90kb .nds. I'm still getting garbage for the filesizes. That alternate method looked perfect, but I can't get it to compile. Is that for C++?


Yeah, it's a c++ library, you have to also use the following header:

#include <fstream>

#156911 - HyperHacker - Thu May 15, 2008 11:50 pm

simonjhall wrote:
I find the formatting of a flash card that goes into a DS will eventually "wear out" for some reason and will need a re-format. Maybe it's that time again?
I suspect that's due to apps using old versions of GBA_NDS_FAT.
_________________
I'm a PSP hacker now, but I still <3 DS.

#156912 - Dwedit - Thu May 15, 2008 11:54 pm

The old GBA_NDS_FAT and GBAMP_CF libraries have serious bugs when enlarging a directory. If you pre-create files so that it does not create anything new, you can avoid many problems.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."