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 filesize with r21

#150021 - cornaljoe - Mon Jan 28, 2008 11:55 pm

Currently I'm using:
Loop:
Code:
fopen
fseek(SEEK_END)
ftell
fclose

Is there a faster way to get multiple filesizes? Thanks for the help.

#150025 - yellowstar - Tue Jan 29, 2008 12:37 am

*EDIT: Well, I don't know about speed... Here's the code I use. I've used this code for a long time, without any problems.*

Here's some code from flipcode:(I've tested it with the DS, and it works)

Code:

GetFileLength(FILE *file)
{
   int l_iSavPos, l_iEnd;

   l_iSavPos = ftell(file);
   fseek(file, 0, SEEK_END);
   l_iEnd = ftell(file);
   fseek(file, l_iSavPos, SEEK_SET);

   return l_iEnd;
}

#150029 - Lazy1 - Tue Jan 29, 2008 1:12 am

I believe stat() is what you are looking for and if I remember correctly it does work on the NDS.

Quick sample, tested MSVC C++ v2002:
Code:

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

int main( int Argc, char* Argv[ ] ) {
   struct stat st;

   if ( stat( "main.cpp", &st ) == 0 ) {
      printf( "File size: %d\n", st.st_size );
      return 0;
   }

   return 1;
}


EDIT:
Forgot multiple, but still this may be faster than opening, closing and seeking for each file.

#150241 - cornaljoe - Thu Jan 31, 2008 10:40 pm

Thanks Lazy1 that did the trick!