Advertisement

Retrieving the size of a file

Started by February 11, 2000 01:10 PM
3 comments, last by bosjoh 24 years, 10 months ago
I always used this function to retrieve the size of a file:

unsigned long filesize(char *filename){
   unsigned long fsize=0;
   unsigned char dummy;
   fstream rfile;//vars
   rfile.open(filename,0x81);
   while(!rfile.eof()){
      fsize++;
	  rfile.read(&dummy,1);
   };
   rfile.close();
   return(fsize);
};
But in visual C++ it doesn''t work properly. Should I use another method?

I always just use:

ifstream fip;

fip.open(fname);

//: seek to the end of file
fip.seekg(0, ios::end);

//: get the position (will be size of file)
unsigned long bytes = fip.tellg();

HTH,

-mordell
__________________________________________

Yeah, sure... we are laughing WITH you ...
Advertisement
you definitely should!

u32 FileLength (FILE *f)
{
return (filelength(fileno(f)));
}

It should be straight-forward to make it accept a filename instead of a file stream (just requires more code).
Darn! It was just my graphics write function. It displayed the numbers in the wrong order.
But thanks anyway mordell, your function is faster than mine (especially in big files).
Foofightr: Your function generates a lot of compiler errors in VC++, just to let you know.
well, I assumed you would know what "u32" was and what header filelength() and fileno() were in. i''ll put it here for your sake

typedef unsigned long u32;
filelength(): #include < io.h>
fileno(): #include < stdio.h>

there, that''s better

This topic is closed to new replies.

Advertisement